-
Sorting a hashmap
Hello,
I am new to java and I want to know how we sort a HashMap. This is the piece of code I have with me.
Code:
private static void Save(String Output) throws Exception
{
PrintWriter pw = new PrintWriter(new File (Release));
Object[] tab = map.keySet().toArray();
for(int i = 0; I <tab.length; I + +)
{
pw.System.out.println(tab[i]+" " + Map.get(tab[i]));
pw.flush();
}
pw.close();
}
I try and create a file "exit" and that is sort in descending order by report. But this is not working for me. If you have some ideas on this it will help me.
-
Re: Sorting a hashmap
Hello,
To sort a HashMap, you can either use a TreeMap or put your values in your set values in a List that you sort. Also in your code if you have an exception then the PrintWriter is not closed. Use something like:
Code:
private static void Save(String Output) throws Exception {
PrintWriter pw = null;
try {
pw = new PrintWriter(new File (Release));
Object[] tab = map.keySet().toArray();
for(int i = 0; I <tab.length; I + +) {
pw.System.out.println(tab[i]+" " + Map.get(tab[i]));
pw.flush();
}
} finally {
if (pw! = null) {
pw.close();
}
}
-
Re: Sorting a hashmap
Hey,
The above posted is correct but I think it needs a modification to the code. I have modified and changed some of the things in the code and the code is now more effective. Here is the code
Code:
Public static void hand(Final String[] args) {
/ / Map Testing
Final Map m = <String, Integer> new HashMap <String, Integer>();
m.could("t1", 1);
m.could("t2", 3);
m.could("t3", 2);
m.could("t4", 3);
/ / Add entries to m a list
Final List <Entry <String, Integer>> entries = new ArrayList <Entry <String, Integer>>(m.entrySet());
/ / Sort the list on the entry value
Collections.fate(entries, new Comparator <Entry <String, Integer>>() {
Public int compares(Final Entry <String, Integer> e1, Final Entry <String, Integer> e2) {
return e1.getValue().compareTo(e2.getValue());
}
});
/ / Display result
for (Final <String, Integer> Entry entry: entries) {
System.out.System.out.println(entry.getKey() + " " + Entry.getValue());
}
}
-
Re: Sorting a hashmap
Hi,
Thank you for your answers but I do not quite understand your code ,in fact as I have said in the beginning that I am a beginner and I know too work with java (if not too much to ask ) m you explain what is the "Entry" t that you put here:
/ / Add en to map a list
Final List <Entry <String, Integer>> en = new ArrayList <Entry <String, Integer>>(map.entrySet());
I also want to understand what is the "e1" and "e2" for you in the sort
Code:
function
/ / Sort the list on the entry value
Collections.fate(en, new Comparator <Entry <String, Integer>>() {
Public int compares(Final Entry <String, Integer> e1, Final Entry <String, Integer> e2) {
return e1.getValue().compareTo(e2.getValue());
}
Any help on this please.
-
Re: Sorting a hashmap
Hello,
Entry is a static class Map. It allows the key-value relationship. The <> are a feature of Java 5: generics. If you do not know what it is, I advise you to read docs on it. The method compares takes two input values. These values are the type of objects in the list. So if the list is composed of one, the method compares needs to take one input. e1 and e2 are just the names of the entries.
e1.getValue () retrieves the value in the combination key / value (ie Entry).
-
Re: Sorting a hashmap
Hello,
The sorting is done in the method compares:
Code:
/ / Sort the list on the entry value
Collections.fate(en, new Comparator <Entry <String, Integer>>() {
Public int compares(Final Entry <String, Integer> e1, Final Entry <String, Integer> e2) {
return e1.getValue().compareTo(e2.getValue());
}
});
I hope your problem is solved.
Page generated in 1,714,034,129.18904 seconds with 11 queries