-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapOrderByValue
More file actions
35 lines (33 loc) · 1.1 KB
/
Copy pathMapOrderByValue
File metadata and controls
35 lines (33 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package collectionImpl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class MapOrderByValue
{
public static void main(String a[]){
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("java", 26);
map.put("C++", 67);
map.put("SQL", 21);
map.put("Unix", 7);
map.put("PHP", 36);
map.put("Perl", 91);
Set<Entry<String, Integer>> set = map.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{
public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
{
return (o1.getValue()).compareTo( o2.getValue() );
}
} );
for(Map.Entry<String, Integer> entry:list){
System.out.println(entry.getKey()+" ==== "+entry.getValue());
}
}
}