0
点赞
收藏
分享

微信扫一扫

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());


作者:​​panie​​

举报

相关推荐

0 条评论