【问题标题】:How to sort java sort map by values with the compare method already in class?java - 如何使用类中已有的比较方法按值对java排序映射进行排序?
【发布时间】:2014-11-02 07:19:46
【问题描述】:

我想按地图的值对地图进行排序。但是比较方法已经在类中了。

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() 方法。我想在比较后按其值对键集进行排序。

【问题讨论】:

标签: java


【解决方案1】:

这是代码 sn-p 你可以观察我是如何实现的

  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);

        }
    }

【讨论】:

【解决方案2】:

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

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());
    }
}

【讨论】:

    猜你喜欢
    • 2013-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    相关资源
    最近更新 更多