-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListToMap.java
More file actions
45 lines (32 loc) · 1.32 KB
/
ListToMap.java
File metadata and controls
45 lines (32 loc) · 1.32 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
/**
*
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;
/**
* @author Subhash
*/
public class ListToMap {
/**
* @param args
*/
public static void main(String[] args) {
List<UserVO> userList = new ArrayList<UserVO>();
userList.add(new UserVO(1, "Subhash", "Java Developer", "Chennai"));
userList.add(new UserVO(2, "John", "Sr.Java Developer", "Chennai"));
userList.add(new UserVO(3, "Reddy", "Java Developer", "Hyderabad"));
userList.add(new UserVO(4, "Raj", "Tester", "Bangalore"));
userList.add(new UserVO(5, "Bharathi", "Quality Engineer", "Chennai"));
userList.add(new UserVO(5, "Alimran", "Quality Engineer", "Chennai"));
Map<Integer,String> userMap=userList.stream().collect(
Collectors.toMap(UserVO::getUserid, UserVO::getUsername,
(oldValue,newValue)->newValue,HashMap::new));
//To solve the duplicated key issue, use the third mergeFunction argument, if the key is duplicated, do we replace with old key or new key?
//if we not used the third mergeFunction argument, we will get the "java.lang.IllegalStateException: Duplicate key xxxxx" Exception.
System.out.println(userMap);
}
}