java8 对于List操作的部分用法

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())

java8学习扩展:

https://blog.csdn.net/y_k_y/article/details/84633001?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.opensearch_close&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.opensearch_close

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值