有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何按值对树集排序?

我对TreeMapTreeSet之类的东西很陌生,想知道如何按值对数据结构进行排序?我意识到使用树集可以自动按字母顺序排序,但我希望它按值排序?你知道怎么做吗

它现在打印出来像

  • aaa:29
  • aaahealthart:30
  • ab:23
  • 艾比:14
  • 腹部:3
  • 香港仔:29
  • 阿伯丁努尼:20

当我想把它打印成

  • aaahealthart:30
  • aaa:29
  • 香港仔:29
  • ab:23
  • 阿伯丁努尼:20
  • 艾比:14
  • 腹部:3

这是我的方法

ArrayList<String> fullBagOfWords = new ArrayList<String>();
public Map<String, Integer> frequencyOne;

public void termFrequency() throws FileNotFoundException{
    Collections.sort(fullBagOfWords);
    Set<String> unique = new TreeSet<String>(fullBagOfWords);
    PrintWriter pw = new PrintWriter(new FileOutputStream(frequencyFile));
    pw.println("Words in Tweets :   Frequency of Words");
    for (String key : unique) {
        int frequency = Collections.frequency(fullBagOfWords, key);

        System.out.println(key + ": " + frequency);
        pw.println(key + ": " + frequency);
        }
    pw.close();
    }

谢谢大家的帮助


共 (3) 个答案

  1. # 1 楼答案

    试试这样:

    Set<Map.Entry<Integer, Integer>> sorted = 
          new TreeSet<Map.Entry<Integer, Integer>>(new Comparator<Map.Entry<Integer, Integer>> {
        public int compare(Map.Entry<Integer, Integer> first, Map.Entry<Integer, Integer> second) {
           return first.getValue().compareTo(second.getValue());
        }
    
        public boolean equals(Map.Entry<Integer, Integer> that) {
            return this.equals(that);
        }
    });
    

    这会给你想要的

  2. # 2 楼答案

    TreeMap按键排序,我认为不能使用相同的实现按值排序。但你可以用稍微不同的方法来完成任务:

    public Map<String, Integer> countWords(List<String> words) {
        Map<String, Integer> result = new Map<>();
        for (String word : words) {
            if (result.containsKey(word)) {
                // the word is already in the map, increment the count
                int count = result.get(word) + 1;
                result.put(word, count);
            } else {
                result.put(word, 1);
            }
        }
    
        return result;
    }
    

    然后你只需要对结果图的元素进行排序。您可以通过以下方式执行此操作:

    public List<Map.Entry<String, Integer> sortMap(Map<String, Integer> map) {
        List<Map.Entry<String, Integer> elements = new LinkedList<>(map.entrySet());
        Collections.sort(elements, new Comparator<Map.Entry<String, Integer>>() {
    
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 ) {
                return o1.getValue().compareTo(o2.getValue());
            }
    
        });
    }
    

    所以你使用第一种方法来计算词频,第二种方法来排序

  3. # 3 楼答案

    您可以创建一个ArrayList并将每个条目存储在其中,如下所示:

    ArrayList<Map.Entry<String, Integer> list = new new ArrayList(map.entrySet());
    

    然后,您可以使用比较器对arrayList进行排序,该比较器根据条目的值对条目进行比较:

    Collections.sort(list , new Comparator<Map.Entry<String, Integer>>() {
    
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 ) {
                return o1.getValue().compareTo(o2.getValue());
            }
    
        });
    

    然后可以打印arrayList中的条目