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

是非成败转头空 青山依旧在 几度夕阳红 。。。
随笔 - 1277, 文章 - 0, 评论 - 214, 阅读 - 321万
  博客园  :: 首页  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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

Posted on   且行且思  阅读(1870)  评论(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;

 

相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
历史上的今天:
2019-05-29 Android SDK
点击右上角即可分享
微信分享提示