明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
  博客园  :: 首页  :: 管理

Java8 List集合如何移除满足条件的元素

Posted on 2023-05-29 22:25  且行且思  阅读(1915)  评论(0)    收藏  举报

1.移除List<String>中指定元素

       for(int i = assSupplementList.size() - 1; i >= 0; i--) {
            TypgHouseOrderAssessmentSupplement item = assSupplementList.get(i);
            if (item.getBzx().contains("新建房屋")) {
                assSupplementList.remove(item);
            }
        }

 

2.移除List<对象>中指定元素

         Iterator<TypgHouseOrderAssessmentSupplement> iterator = assSupplementList.iterator();
         while(iterator.hasNext()) {
             TypgHouseOrderAssessmentSupplement item = iterator.next();
             if (item.getBzx().contains("新建房屋")) {
                 iterator.remove();
            }
         }

 

3、使用Java8中的lambda表达式过滤

    //从list中过滤出,list字段中不包含某个字符串的数据
    //list的yourStr字段的值:“aaa,bbb,ccc”,XXArr()属性的值是:“aaa,ccc”
    List<YourBean> list = xxService.findList(yourBean);
    for (String index : beanName.getXXArr()) {
        list = list.stream().filter(item -> item.getYourStr().indexOf(index) < 0).collect(Collectors.toList());
    }
    return list;