removeIf用法

  • 删除list中包含某个字符的对象
// 创建一个动态数组
ArrayList<String> sites = new ArrayList<>();
sites.add("Taobao");
 // 删除名称中带有 Tao 的元素
sites.removeIf(e -> e.contains("Tao"));

  • 删除list中,某个对象中某个属性满足某个条件的

Collection<Person> collection = new ArrayList();
collection.add(new Person("张三", 22, "男"));
collection.removeIf(
	person -> person.getAge() >= 20
);
  • 删除map 里面某个值。 比如, 某个班级里面,年龄大于20 的
Map<Integer, List<Person>> usersSessionMap = new HashMap<>();
usersSessionMap.values().forEach(list -> list.removeIf(s -> s.getAge() >= 20));


// 通过key移除
usersSessionMap.keySet().removeIf(key -> key != 1);
// 通过键/值的输入/组合删除
usersSessionMap.entrySet().removeIf(entry -> entry.getValue()==null||entry.getValue().isEmpty());
posted @   panie2015  阅读(376)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示