-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortMapByValue.java
More file actions
48 lines (32 loc) · 1.13 KB
/
SortMapByValue.java
File metadata and controls
48 lines (32 loc) · 1.13 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
36
37
38
39
40
41
42
43
44
45
46
47
48
package collection;
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 SortMapByValue {
public static void main(String[] args) {
Map<String,String> mapString = new HashMap<>();
mapString.put("1", "vaibhav");
mapString.put("2", "rahul");
mapString.put("3", "piyush");
mapString.put("4", "harshit");
mapString.put("5", "yogesh");
sortMapUsingValue(mapString);
}
private static void sortMapUsingValue(Map<String, String> mapString) {
Set<Map.Entry<String, String>> entryMap = mapString.entrySet();
List<Map.Entry<String,String>> entryMapList = new ArrayList<Entry<String,String>>(entryMap);
Collections.sort(entryMapList,new Comparator<Entry<String,String>>() {
@Override
public int compare(Entry<String, String> o1, Entry<String, String> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
System.out.println(mapString);
System.out.println(entryMapList);
}
}