-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortHashMap.java
More file actions
45 lines (36 loc) · 1.22 KB
/
SortHashMap.java
File metadata and controls
45 lines (36 loc) · 1.22 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
package org.simplemedium;
import java.util.*;
public class SortHashMap {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
LinkedHashMap<String, String> sortedMap = new LinkedHashMap<>();
ArrayList<String> list = new ArrayList<>();
map.put("2", "B");
map.put("8", "A");
map.put("4", "D");
map.put("7", "F");
map.put("6", "W");
map.put("19", "J");
map.put("1", "Z");
for(Map.Entry<String, String> iMap : map.entrySet()) {
list.add(iMap.getValue());
}
System.out.println(list);
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
System.out.println(list);
for(String str: list) {
for(Map.Entry<String, String> iMap2 : map.entrySet()) {
if(iMap2.getValue().equals(str)) {
sortedMap.put(iMap2.getKey(), iMap2.getValue());
}
}
}
System.out.println("Original " + map);
System.out.println("Sorted " + sortedMap);
}
}