Jump to content

Java sorting LinkedList with Map.Entry objects

Satlen
Go to solution Solved by Mr_KoKa,

LinkedList<Map.Entry> entryList = new LinkedList(); //YourlistCollections.sort(list, new Comparator<Map.Entry>() {     @[member='OverRide']     public int compare(Map.Entry e1, Map.Entry e2) {         return //Helper function goes here, or just split it into if and many returns.     }});
Your return value needs to be either a negative integer, zero, or a positive integer as the first argument (e1) is less than, equal to, or greater than the second (e2).

 

You can make yourself a helper function that will compare two entries and return integer so you can use it inline with return like, return yourCompareFunction(e1, e2);

So I am kind of stuck right now, I am working on an assignment and I need to take the entries of a map and put them into a linked list, then sort them based on the keys and again based on the values. The program takes the text of an input file and counts the occurrences of each word. I am stuck on actually figuring out how to sort (or compare) the entries based on keys and values. I do not need to write my own sort. Here is my code:

import java.util.*;import java.io.*;class Main{    public static void main(String[] args){    //setup new hashmap with string keys and int values    HashMap<String, Integer> wordHashMap = new HashMap<>();    //attempt to open the file    try{    	Scanner reader = new Scanner(new File("input.txt"));        //loop until the end of file has been reached        while(reader.hasNext()){          //set string to next word          String nextWord = reader.next().toLowerCase();          //remove any non-alphabetic characters          nextWord = nextWord.replaceAll("[^a-zA-Z]", "");          //if the key already exists, just update its value by 1          if(wordHashMap.containsKey(nextWord)){            wordHashMap.put(nextWord, wordHashMap.get(nextWord) + 1);          }          else{            wordHashMap.put(nextWord, 1);          }        }      }    //catch any errors that might occurr when trying to read the file    catch(IOException e){    	e.printStackTrace();    }    System.out.println("==HashMap====================================");    System.out.println(wordHashMap.entrySet());    Set<Map.Entry<String, Integer>> entrySetHash = wordHashMap.entrySet();    for(Map.Entry e : entrySetHash){      System.out.println(e.toString());    }    System.out.println("==TreeMap====================================");    TreeMap<String, Integer> wordTreeMap = new TreeMap<>(wordHashMap);    System.out.println(wordTreeMap.entrySet());    Set<Map.Entry<String, Integer>> entrySetTree = wordTreeMap.entrySet();    for(Map.Entry e : entrySetTree){      System.out.println(e.toString());    }    System.out.println("==LinkedList Alphabetic Sort=================");    LinkedList<Map.Entry> entryList = new LinkedList<>();    for(Map.Entry e : entrySetTree){      entryList.add(e);    }    //not really sure how to sort here    System.out.println("==LinkedList Numeric Sort====================");  }}

Am I missing something that will allow me to use Collections.sort for the key and value stored in each entry?

Link to comment
Share on other sites

Link to post
Share on other sites

LinkedList<Map.Entry> entryList = new LinkedList(); //YourlistCollections.sort(list, new Comparator<Map.Entry>() {     @[member='OverRide']     public int compare(Map.Entry e1, Map.Entry e2) {         return //Helper function goes here, or just split it into if and many returns.     }});
Your return value needs to be either a negative integer, zero, or a positive integer as the first argument (e1) is less than, equal to, or greater than the second (e2).

 

You can make yourself a helper function that will compare two entries and return integer so you can use it inline with return like, return yourCompareFunction(e1, e2);

Link to comment
Share on other sites

Link to post
Share on other sites

LinkedList<Map.Entry> entryList = new LinkedList(); //YourlistCollections.sort(list, new Comparator<Map.Entry>() {     @[member='OverRide']     public int compare(Map.Entry e1, Map.Entry e2) {         return //Helper function goes here, or just split it into if and many returns.     }});
Your return value needs to be either a negative integer, zero, or a positive integer as the first argument (e1) is less than, equal to, or greater than the second (e2).

 

You can make yourself a helper function that will compare two entries and return integer so you can use it inline with return like, return yourCompareFunction(e1, e2);

 

 

Yeah I ended up finding out that in java 1.8 they actually have a method that returns a comparator ( every time I tried using it, it gave me some odd error. I ended up doing as you replied here and just created a comparator class and overrode the compare method. I did not realize that you could do this from the sort parameters like you have though. Thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×