java stream 流处理数据
Map 类型或者 List类型的数据流式处理
过滤
List<Integer> collect = users.keySet().stream().filter(a -> {
return a > 2 ;
}).collect(Collectors.toList());
return a > 2 ;
}).collect(Collectors.toList());
过滤后转map
Map<Integer, User> collect1 = users.keySet().stream().filter(a -> {
return a > 2;
}).collect(Collectors.toMap(a -> a, b -> users.get(b)));
Map<Integer, User> collect2 = users.keySet().stream().filter(a -> {
return a > 2;
}).collect(Collectors.toMap(Function.identity(), users::get, (a, b) -> (b)));
return a > 2;
}).collect(Collectors.toMap(a -> a, b -> users.get(b)));
Map<Integer, User> collect2 = users.keySet().stream().filter(a -> {
return a > 2;
}).collect(Collectors.toMap(Function.identity(), users::get, (a, b) -> (b)));
转map换key
Map<String, Integer> collect3 = users.keySet().stream().filter(a -> {
return a > 2;
}).collect(Collectors.toMap(a -> users.get(a).getName(), b -> users.get(b).getId()));
return a > 2;
}).collect(Collectors.toMap(a -> users.get(a).getName(), b -> users.get(b).getId()));
获取更多value的内容,并转换key,value
Map<String, Integer> collect4 = users.keySet().stream().filter(a -> {
return a > 2;
}).map(b -> users.get(b)).collect(Collectors.toMap(User::getName, User::getId, (a, b) -> a));
return a > 2;
}).map(b -> users.get(b)).collect(Collectors.toMap(User::getName, User::getId, (a, b) -> a));
Map<String, String> collect5 = users.keySet().stream().filter(a -> {
return a > 2;
}).map(b -> users.get(b)).collect(Collectors.toMap(a -> a.getName(), b -> b.toString()));
return a > 2;
}).map(b -> users.get(b)).collect(Collectors.toMap(a -> a.getName(), b -> b.toString()));