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 @ 2022-11-15 15:20  panie2015  阅读(341)  评论(0编辑  收藏  举报