使用LinkedList和sort方法

时间:2015-10-29 17:39:27

标签: java sorting linked-list treemap

我目前正在尝试使用LinkedList将密钥列表从最高出现次数打印到最低出现次数。我试图在条目的getValue方法上使用sort方法,但它不起作用。我有什么想法我做错了。这是我的代码片段

// Beginning tree map
        Map<String, Integer> map1 = new TreeMap<>();
        String[] words1 = text.split("[ \n\t\r.,;:!?(){ ]");
        for (int i = 0; i < words1.length; i++) 
        {
            String key = words1[i].toLowerCase();

            if (key.length() > 0) 
            {
                if (!map1.containsKey(key)) 
                {
                    map1.put(key, 1);
                }
                else 
                {
                    int value = map1.get(key);
                    value++;
                    map1.put(key, value);
                }
            }
        }
        Set<Map.Entry<String, Integer>> entrySet1 = map1.entrySet();
        // Get key and value from each entry
        System.out.println("Treemap: " + map1);
        for (Map.Entry<String, Integer> entry: entrySet1)
            System.out.println(entry.getValue() + "\t" + entry.getKey());
        System.out.println("");
        // Beginning for LinkedList
        LinkedList<Entry<String, Integer>> linkedList = new LinkedList<>(entrySet1);
        System.out.println("linkedList:");
        System.out.println(linkedList);
        System.out.println(linkedList.sort(entrySet1.getKey());

用我当前的输出

Treemap: {a=2, class=1, fun=1, good=3, have=3, morning=1, visit=1}
2   a
1   class
1   fun
3   good
3   have
1   morning
1   visit

linkedList:
[a=2, class=1, fun=1, good=3, have=3, morning=1, visit=1]

所以我的最终问题是,如何将我的getValue方法传递给LinkedList,以便按排序顺序打印它们。

0 个答案:

没有答案