list转为Map
Map<String,User> userMap = list.stream().collect(Collectors.toMap(User::getName, a -> a,(k1, k2)->k1));
System.out.println(userMap);
foreach循环
User user=new User();
user.setName("good");
user.setAddr("dd");
User user2=new User();
user2.setName("goodbye");
user2.setAddr("dda");
List<User> list=new ArrayList<>();
list.add(user);
list.add(user2);
list.stream().forEach(userObj->{
System.out.println(userObj.getName());
});
将List分组:List里面的对象元素,以某个属性来分组
Map<String, List<User>> groupBy = list.stream().collect(Collectors.groupingBy(User::getName));
System.out.println(groupBy);
过滤:从集合中过滤出来符合条件的元素
List<User> filterList = list.stream().filter(user3 -> user3.getName().equals("good")).collect(Collectors.toList());
filterList.stream().forEach(user3 ->{
System.out.println(user3.getName());
});
List中对象属性值作为 Map的ke和value
Map<String,String> userMap2 = list.stream().collect(Collectors.toMap(User::getName, User::getAddr,(k1, k2)->k1));
System.out.println(userMap2);