Continue to create, accelerate growth! This is the 4th day of my participation in the "Nuggets Daily New Plan · June Update Challenge", click to view the details of the event
- We have a UserInfo class
Stream operations through JAVA8 need to be converted into a map with userId as key and name as value.
public class User {
private Integer userId;
private String name;
private String email;
public User(Integer userId, String name, String email) {
this.userId = userId;
this.name = name;
this.email = email;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", name='" + name + ''' +
", email='" + email + ''' +
'}';
}
}
复制代码
- implementation code
public class ListToMap {
public static void main(String[] args) {
List<User> users = new ArrayList<>();
users.add(new User(1, "user1", "[email protected]"));
users.add(new User(2, "user2", "[email protected]"));
users.add(new User(3, "user3", "[email protected]"));
users.add(new User(4, "user4", "[email protected]"));
Map<Integer, String> userIdAndName = users.stream()
.collect(Collectors.toMap(User::getUserId, User::getName));
System.out.println(userIdAndName);
}
}
复制代码
output result
- userId is key, user object is value
public class ListToMap {
public static void main(String[] args) {
List<User> users = new ArrayList<>();
users.add(new User(1, "user1", "[email protected]"));
users.add(new User(2, "user2", "[email protected]"));
users.add(new User(3, "user3", "[email protected]"));
users.add(new User(4, "user4", "[email protected]"));
Map<Integer, User> userIdAndUser = users.stream()
.collect(Collectors.toMap(User::getUserId, user-> user));
System.out.println(userIdAndUser);
}
}
复制代码
output
- Duplicate key handling
During the conversion process, if the same key occurs, a duplicate key exception will be thrown.
We changed the userId of 3 to 2, which conflicts with the second user
Collectors.toMap supports a third parameter to select duplicate values
public class ListToMap {
public static void main(String[] args) {
List<User> users = new ArrayList<>();
users.add(new User(1, "user1", "[email protected]"));
users.add(new User(2, "user2", "[email protected]"));
users.add(new User(2, "user3", "[email protected]"));
users.add(new User(4, "user4", "[email protected]"));
Map<Integer, User> userIdAndUser = users.stream()
.collect(Collectors.toMap(User::getUserId, user-> user, (oldValue, newValue) -> newValue));
System.out.println(userIdAndUser);
}
}
复制代码
Select the new value to overwrite the old value and get the result
Only user3 will appear, and user2 will be overwritten.
- Mapping IDs and user lists
The duplicate IDs that appeared above are overwritten according to the value, if in some cases it needs to be mapped to a list.
That is: the case of List -> Map<Integer, List>
This requires the use of groupingBy
public class ListToMap {
public static void main(String[] args) {
List<User> users = new ArrayList<>();
users.add(new User(1, "user1", "[email protected]"));
users.add(new User(2, "user2", "[email protected]"));
users.add(new User(2, "user3", "[email protected]"));
users.add(new User(4, "user4", "[email protected]"));
Map<Integer, List<User>> userMap = users.stream()
.collect(Collectors.groupingBy(User::getUserId));
System.out.println(userMap);
}
}
复制代码
There are multiple pieces of data with ID 2.
- Specify the Map type
Collectors.toMap supports the fourth parameter to instantiate the map type
public class ListToMap {
public static void main(String[] args) {
List<User> users = new ArrayList<>();
users.add(new User(1, "user1", "[email protected]"));
users.add(new User(2, "user2", "[email protected]"));
users.add(new User(3, "user3", "[email protected]"));
users.add(new User(4, "user4", "[email protected]"));
Map<Integer, User> userIdAndUser = users.stream()
.collect(Collectors.toMap(User::getUserId, user-> user, (oldValue, newValue) -> newValue, ConcurrentHashMap::new));
System.out.println(userIdAndUser);
System.out.println(userIdAndUser.getClass());
}
}
复制代码
output
The type of userIdAndUser class is ConcurrentHashMap