I am currently trying to use a LinkedList to print a list of keys from their highest number of occurences to lowest. I am trying to use the sort method on the getValue method of the entry but it is not working. Any ideas what I'm doing wrong. Here is a snippet of my code
// 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());
With my current output
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]
So my ultimate question is, how can i pass my getValue method to the LinkedList in order to print them in a sorted order.