首页 > 其他分享 >removeIf用法

removeIf用法

时间:2022-11-15 15:36:17浏览次数:45  
标签:删除 usersSessionMap list 用法 某个 new 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());

标签:删除,usersSessionMap,list,用法,某个,new,removeIf
From: https://www.cnblogs.com/panie2015/p/16892537.html

相关文章