list map 处理方式

List 实体中获取 摸个字段的集合

// A code block
Set<Long> taskIds = orderList.stream().map(EDeliverOrderDO::getTaskId).collect(Collectors.toSet());

List里面的对象元素,以某个属性来分组,例如,以id分组,将id相同的放在一起:

// list
List<Apple> appleList = new ArrayList<>();//存放apple对象集合
Apple apple1 =  new Apple(1,"苹果1",new BigDecimal("3.25"),10);
Apple apple12 = new Apple(1,"苹果2",new BigDecimal("1.35"),20);
Apple apple2 =  new Apple(2,"香蕉",new BigDecimal("2.89"),30);
Apple apple3 =  new Apple(3,"荔枝",new BigDecimal("9.99"),40);
 
appleList.add(apple1);
appleList.add(apple12);
appleList.add(apple2);
appleList.add(apple3);
// 分组
Map<Integer, List<Apple>> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId));

将list转换map(key和Bean的形式)

// A code block
Map<String, BazTgTO> tgMap = tgList.stream().collect(Collectors.toMap(BazTgTO::getTgId, n -> n));

将list转换map(key和value是实体的属性)

// A code block
Map<String, String> map = list.stream().collect(Collectors.toMap(Person::getId, Person::getName));

将list 根据字段移除对象

// A code block
List list = new ArrayList<>();
list.add(new User(1,"张三"));
list.add(new User(4,"赵六"));
//条件删除
list.removeIf(user -> user.getUserId() == 4);

将list 排序

Collections.sort(personVOList, new Comparator<personVO>() {
         public int compare(personVO arg0, personVO arg1) {
            return arg0.getUnitCountBegin().compareTo(arg1.getUnitCountBegin());
         }
      });

list 过滤

// 过滤查询 level 层级为1的

		// 第一种写法
        List<ArticleClassDO> firstArticleClassList = articleClassDOList.stream().filter(p -> 1 == p.getTreeLevel()).collect(Collectors.toList());
      
        // 第二种写法
		List<ArticleClassDO> firstArticleClassList = articleClassDOList.stream().filter(new Predicate<ArticleClassDO>() {
            public boolean test(ArticleClassDO articleClassDO) {
                return articleClassDO.getTreeLevel() == 1;
            }
        }).collect(Collectors.toList());

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值