List list = new ArrayList();
//list中去重:
List<Long> collect = skuViewIdList.stream().distinct()
.collect(Collectors.toList());
//list中根据某个属性去重:
List<UserInfoVo> collect = userResult.stream().collect(Collectors
.collectingAndThen(Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing(UserInfoVo::getUserId))), ArrayList::new));
//list中根据某个属性排序 升序降序:
List<UserInfoVo> userResult =userInfoVos.stream()
.sorted(Comparator.comparing(UserInfoVo::getUserId))
.collect(Collectors.toList());
List<UserInfoVo> userResult =userInfoVos.stream()
.sorted(Comparator.comparing(UserInfoVo::getUserId).reversed())
.collect(Collectors.toList());
//自定义排序:先按姓名升序,姓名相同则按年龄升序
List<Student> collect = studentList.stream().sorted(
(o1, o2) -> {
if (o1.getName().equals(o2.getName())) {
return o1.getAge() - o2.getAge();
} else {
return o1.getName().compareTo(o2.getName());
}
}
).collect(Collectors.toList());
//list中根据某个属性过滤并求和:
List<UserInfoVo> userResult = userInfoVos.stream()
.filter(x ->x.getStatus().equals(1))
.map(MDistributionConsumerOrdersVo::getRebate)
.reduce(BigDecimal::add).get();
//list中根据某个属性过滤:
List<UserInfoVo> userResult = userInfoVos.stream()
.filter(x -> x.getStatus().equals(1))
.collect(Collectors.toList());
//List userList和List personList
想通过User的id和Person的id进行去重,去掉userList中的User的id不等于personList中的Person的id的对象
List<User> userList= userList.stream()
.filter(user-> !personList.stream()
.map(person ->person.getId())
.collect(Collectors.toList())
.contains(user.getId()))
.collect(Collectors.toList());
如果在for循环里面编写查询接口速度较慢,可以使用.parallelStream()方法java8的并行流(里面的顺序会发生变化):
userList.parallelStream()skuViewIdList.stream()
.collect(Collectors.toList())