Map<String,Object> map=new HashMap<>();
map.put("name","李四");
map.put("age","12");
Map<String,Object> map1=new HashMap<>();
map1.put("name","张三");
map1.put("age","12");
Map<String,Object> map2=new HashMap<>();
map2.put("name","李四");
map2.put("age","15");
List<Map<String,Object>> list=new ArrayList<>();
list.add(map1);
list.add(map2);
list.add(map);
//对list<Map<String,Object>>去重
list = list.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(m->m.get("name").toString()))
),ArrayList::new
)
);
//对list<Object>去重
List<Test> unique = list.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Test::getName))), ArrayList::new)
);
list.stream().forEach(item->{
for (Map.Entry<String,Object> m:item.entrySet()){
System.out.println(m.getKey()+":"+m.getValue());
}
});
//map取出 key value
Map<String,String> map = new HashMap<>();
map.put("name","zhangsan");
map.forEach((key, val) -> {
System.out.println(key+":"+val);
});
//判断是否存在,存在返回true
boolean isBankGjdq = qbSsTjwjqkWqzbs.stream().filter(qbSsTjwjqkWqzb -> CommonResources.WQZB_SJSJ.getName().equals(qbSsTjwjqkWqzb.getJgmc())).findAny().isPresent();
//跳过指定个数的Stream中元素,获取后面的元素。
list.stream().skip(1).collect(Collectors.toList());
//取出某个字段的值
list.stream().map(user2 -> user2.getId()).collect(Collectors.toList());
//对指定字段求和
list.stream().collect(Collectors.summarizingInt(User::getAge));
//list转map
Map<Integer, User> map = list.stream().collect(Collectors.toMap(user2 -> user2.getId(), user2 -> user2));
//对Stream中的元素进行过滤操作,当设置条件返回true时返回相应元素。
list.stream().filter(uu -> uu.getAge() == 18).collect(Collectors.toList());
Stream操作
于 2021-11-19 13:21:06 首次发布