1庶喜、JDK1.8流方式
用戶類
import lombok.Data;
@Data
public class User {
private String id;
private String name;
public User(String id, String name) {
this.id = id;
this.name = name;
}
}
1. 收集成key為id,value為name:
List<User> userList = new ArrayList<>();
User user1 = new User("1", "name1");
User user2 = new User("2", "name2");
User user3 = new User("3", "name3");
userList.add(user1);
userList.add(user2);
userList.add(user3);
Map<String, String> collect = userList.stream().collect(Collectors.toMap(User::getId, User::getName));
for (Map.Entry<String, String> next : collect.entrySet()) {
System.out.println("key: " + next.getKey() + " , " + "value: " + next.getValue());
}
2. 收集成實體本身
List<User> userList = new ArrayList<>();
User user1 = new User("1", "name1");
User user2 = new User("2", "name2");
User user3 = new User("3", "name3");
userList.add(user1);
userList.add(user2);
userList.add(user3);
Map<String, User> collect = userList.stream().collect(Collectors.toMap(User::getId, user -> user));
for (Map.Entry<String, User> next : collect.entrySet()) {
System.out.println("key: " + next.getKey() + " , " + "value: " + next.getValue());
}
user -> user是一個返回本身的lambda表達式隶校,其實還可以使用Function接口中的一個默認方法代替缔杉,使整個方法更簡潔優(yōu)雅:
Map<String, User> collect1 = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));
重復key的情況:
代碼如下:
Map<String, User> collect2 = userList.stream().collect(Collectors.toMap(User::getName, Function.identity());
這個方法可能報錯(java.lang.IllegalStateException: Duplicate key),因為name是有可能重復的涯呻。
toMap有個重載方法,可以傳入一個合并的函數(shù)來解決key沖突問題:
List<User> userList = new ArrayList<>();
User user1 = new User("1", "name2");
User user2 = new User("2", "name2");
User user3 = new User("3", "name3");
userList.add(user1);
userList.add(user2);
userList.add(user3);
Map<String, User> collect = userList.stream().collect(Collectors.toMap(User::getName, Function.identity(), (key1, key2) -> key2));
for (Map.Entry<String, User> next : collect.entrySet()) {
System.out.println("key: " + next.getKey() + " , " + "value: " + next.getValue());
}
這里只是簡單的使用后者覆蓋前者來解決key重復問題腻要。還有一種分組的方法:
List<User> userList = new ArrayList<>();
User user1 = new User("1", "name2");
User user2 = new User("2", "name2");
User user3 = new User("3", "name3");
userList.add(user1);
userList.add(user2);
userList.add(user3);
Map<String, List<User>> collect3 = userList.stream().collect(Collectors.groupingBy(User::getName));
for (Map.Entry<String, List<User>> next : collect3.entrySet()) {
System.out.println("key: " + next.getKey() + " , " + "value: " + next.getValue());
}
3. 指定具體收集的map
toMap還有另一個重載方法复罐,可以指定一個Map的具體實現(xiàn),來收集數(shù)據(jù):
List<User> userList = new ArrayList<>();
User user1 = new User("1", "name2");
User user2 = new User("2", "name2");
User user3 = new User("3", "name3");
userList.add(user1);
userList.add(user2);
userList.add(user3);
Map<String, User> collect3 = userList.stream().collect(Collectors.toMap(User::getName, Function.identity(), (key1, key2) -> key2, LinkedHashMap::new));
for (Map.Entry<String, User> next : collect3.entrySet()) {
System.out.println("key: " + next.getKey() + " , " + "value: " + next.getValue());
}