users.stream().forEach(user -> {
System.out.print(+user.getId()+"--"+user.getName()+" ");
});
获取实体类对象中某一个字段转为List
List<Long> userIds = users.stream().map(User::getId).collect(Collectors.toList());
根据单个字段字段去除重复
List<User> userList = users.stream() .collect(
Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(user -> user.getName()))), ArrayList::new));
根据多个字段去除重复
List<User> userList = users.stream() .collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(user -> user.getName()+";"+user.getId()))), ArrayList::new));
List去重两个相同的实体类对象或者相同的单个对象字段
List<String> classNameList = new ArrayList(new HashSet(userList));
根据某个字段分组
users.stream().collect(Collectors.groupingBy(User::getName))