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))
本文介绍如何使用 Java 的 Stream API 进行集合操作,包括转换实体类字段为列表、去除重复元素、按单个或多个字段去重、以及根据指定字段进行分组等实用技巧。

6628

被折叠的 条评论
为什么被折叠?



