-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortingMap.java
More file actions
67 lines (56 loc) · 1.96 KB
/
SortingMap.java
File metadata and controls
67 lines (56 loc) · 1.96 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package Collections;
import java.util.*;
import java.util.stream.Collectors;
/**
* The Sorting map.
* Sorting map of object, based on the element in the object.
*/
public class SortingMap {
/**
* The type Employees.
*/
static class Employees {
/**
* The Name.
*/
String name;
/**
* The Number.
*/
int number;
/**
* Instantiates a new Employees.
*
* @param name the name
* @param number the number
*/
public Employees(String name, int number) {
this.name = name;
this.number = number;
}
}
/**
* The entry point of application.
*
* @param args the input arguments
*/
public static void main(String[] args) {
Map<String, Employees> employeesMap = new HashMap<>();
employeesMap.put("123", new Employees("raju", 2565));
employeesMap.put("345", new Employees("bob", 2595));
employeesMap.put("563", new Employees("tommy", 0065));
employeesMap.put("103", new Employees("vasu", 2865));
employeesMap.put("193", new Employees("rayan", 6765));
employeesMap.put("903", new Employees("moni", 9089));
employeesMap.put("883", new Employees("koppan", 4567));
List<Map.Entry<String, Employees>> entryList = new ArrayList<>(employeesMap.entrySet());
Collections.sort(entryList, new Comparator<Map.Entry<String, Employees>>() {
@Override
public int compare(Map.Entry<String, Employees> o1, Map.Entry<String, Employees> o2) {
return o1.getValue().name.compareTo(o2.getValue().name);
}
});
employeesMap = entryList.stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
employeesMap.entrySet().stream().forEach(e -> System.out.println(e.getKey() + " : " + e.getValue().name));
}
}