如何使用类中已有的compare方法按值排序java排序映射?

时间:2014-11-02 07:19:46

标签: java

我想按照其值对地图进行排序。但比较方法已经在课堂上了。

public class Parser implements Comparator<String> {

    private Map<String, Integer> frequencies;

    public Parser() {
        frequencies = new HashMap<String, Integer>();
    }


    public int getCount(String word) {
        Integer c = frequencies.get(word);
        if (c == null) {
            return 0;
        }
        else {
            return c.intValue();
        }
    }


    public int compare(String o1, String o2) {
        int count1 = getCount(o1);
        int count2 = getCount(o2);
        return count1 < count2 ? -1 : count1 > count2 ? 1 : 0;
    }

    public List<String> getWordsInOrderOfFrequency(){
        TreeMap<String,Integer> sorted_map = new TreeMap<String,Integer>();
        sorted_map.putAll(frequencies);
        ArrayList<String> result = new ArrayList<String>(sorted_map.keySet());
        return result;
        }
}

这里的问题在于getWordsInOrderOfFrequenct()方法。我希望在比较后按键值对键集进行排序。

2 个答案:

答案 0 :(得分:0)

以下是您可以观察我是如何实现

的代码段
  public class WordFrequency {
        public static String sentence = "one three two two three three four four four";
        public static Map<String, Integer> map;

        public static void main(String[] args) {
            map = new HashMap<>();
            String[] words = sentence.split("\\s");

            for (String word : words) {
                Integer count = map.get(word);
                if (count == null) {
                    count = 1;
                } else {
                    ++count;
                }
                map.put(word, count);
            }

            Comparator<String> myComparator = new Comparator<String>() {

                @Override
                public int compare(String s1, String s2) {
                    if (map.get(s1) < map.get(s2)) {
                        return -1;
                    } else if (map.get(s1) > map.get(s2)) {
                        return 1;
                    } else {
                        return s1.compareTo(s2);
                    }
                }

            };
            SortedMap<String, Integer> sortedMap = new TreeMap<String, Integer>(myComparator);
            System.out.println("Before sorting: " + map);
            sortedMap.putAll(map);
            System.out.println("After Sorting based on value:" + sortedMap);

        }
    }

答案 1 :(得分:0)

我认为诀窍是提取比较方法的行为,以便可以在排序中重复使用...

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.google.common.collect.Lists.*;

public class Parser implements Comparator<String> {

    private Map<String, Integer> frequencies = new HashMap<String, Integer>();

    public int getCount(String word) {
        Integer c = this.frequencies.get(word);
        if (c == null) {
            return 0;
        }
        else {
            return c.intValue();
        }
    }

    public int compare(String o1, String o2) {
        return staticCompare(getCount(o1), getCount(o2));
    }

    private static int staticCompare(int count1, int count2) {
        return count1 < count2 ? -1 : count1 > count2 ? 1 : 0;
    }

    public List<String> getWordsInOrderOfFrequency(){
        List<Map.Entry<String, Integer>> entries = newArrayList(frequencies.entrySet());
        Collections.sort(entries, (o1, o2) -> staticCompare(getCount(o1.getKey()), getCount(o2.getKey())));
        return entries.stream().map(entry -> entry.getKey()).collect(Collectors.toList());
    }
}