java8-笔记
获取某个字段的值
List<Integer> num = modelList.stream().map(model::getID).collect(Collectors.toList());
根据某个字段去重再获取某个字段的值。
List<CallBillModel> studentList = CBModels.stream()
.collect(Collectors.collectingAndThen(
Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(CallBillModel::getCallUuid))),
ArrayList::new));
去重,取最后一条
hisLabReportModelList = new ArrayList<>(hisLabReportModelList.stream()
.collect(Collectors.toMap(HisLabReportModel::getCheckNo, Function.identity(), (key1, key2) -> key2))
.values());
根据某个字段 分组构造数据、统计数量,然后排序
List<WardDurationCall> durationNameList = wardCallStatistics.getDurations().stream()
.collect(Collectors.groupingBy(
item -> StringHelper.format("{0}_{1}", item.getDurationName(), item.getDurationOrder()),
Collectors.counting()))
.entrySet().stream().map(item ->
{
WardDurationCall wardDurationCall = new WardDurationCall();
String[] keyData = item.getKey().split("_");
wardDurationCall.setDurationName(keyData[0]);
wardDurationCall.setCount(item.getValue());
wardDurationCall.setDurationOrder(Integer.parseInt(keyData[1]));
return wardDurationCall;
}).sorted(Comparator.comparing(WardDurationCall::getDurationOrder))
.collect(Collectors.toList());
//正向
final Map<String, Integer> sortedByCount3 = wordCounts.entrySet()
.stream()
.sorted((e1, e2) -> e1.getValue().compareTo(e2.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
//{software=50, technology=70, USA=100, jobs=200, opportunity=200}
//反向 == reversed()
final Map<String, Integer> sortedByCount2 = wordCounts.entrySet()
.stream()
.sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
//{jobs=200, opportunity=200, USA=100, technology=70, software=50}
// 根据name,sex两个属性去重
List<Person> unique = persons.stream().collect(
Collectors. collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + ";" + o.getSex()))), ArrayList::new)
// 根据name去重
List<Person> unique = persons.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new)
);
// 删除ResultDateTime 字段为null的数据
hisLabReportDetail.removeIf(d -> NullChecker.isNull(d.getResultDateTime()));
//Java8中 map.values(Integer) 求和
Integer totalCount = map.values().stream().mapToInt(Integer::intValue).sum(); //values求和
//Map遍历
map.entrySet().stream().forEach(x -> {
System.out.println("key: "+x.getKey()+", value: "+x.getValue());
});
//Map 过滤:
result = map.entrySet().stream()
.filter(map -> "hello world".equals(map.getValue()))
.map(map -> map.getValue())
.collect(Collectors.joining()
);
//// 多个Map<Long,Integer>, 根据key相同的,value累积求和;
public static Map mapCombine(List<Map<Long,Integer>> list) {
Map<Long, Integer> map = new HashMap<>();
for (Map<Long,Integer> m : list) {
Iterator<Long> it = m.keySet().iterator();
while (it.hasNext()) {
Long key = it.next();
if (!map.containsKey(key)) {
map.put(key, m.get(key));
} else {
map.put(key,(map.get(key)+m.get(key)));
}
}
}
return map;
}
//过滤两个list中相同的属性值的数据
List<Map<String,Object>> list3 = list1.stream().parallel().filter(a -> list2.stream()
.noneMatch(b-> Objects.equals(a.get("age"), b.get("age")))).collect(Collectors.toList());
//过滤两个list中相同的属性值的数据
List<Map<String,Object>> list3 = list1.stream().parallel().filter(a -> list2.stream()
.anyMatch(b-> Objects.equals(a.get("age"), b.get("age")))).collect(Collectors.toList());
//map 转list<String>
List<String> data= maps.entrySet().stream().map(et -> et.getKey() + "_" + et.getValue())
.collect(Collectors.toList());
//list<Object>转map
Map<Integer, String> result1 = list.stream().collect(
Collectors.toMap(Hosting::getId, Hosting::getName));
//list<String>转 map List<String> names = Lists.newArrayList("Answer", "AnswerAIL", "AI");
Map<String, Object> mapData = names.stream().collect(Collectors.toMap(k -> k, v -> v));
//程序运行输出{Answer=Answer, AnswerAIL=AnswerAIL, AI=AI}
//将 List 转为 Map<K, V>
public static void main(String[] args) throws Exception {
List<User> users = new ArrayList<>();
for (int i = 0; i < 3; i++) {
users.add(new User("answer" + i, new Random().nextInt(100)));
}
System.out.println(JSON.toJSONString(users));
System.out.println();
Map<String, Integer> map = users.stream().collect(Collectors.toMap(User::getName, User::getAge));
System.out.println(map);
}
//[{"age":78,"name":"answer0"},{"age":89,"name":"answer1"},{"age":72,"name":"answer2"}]
//{answer2=72, answer1=89, answer0=78}
/**
Map集合 key value
HashMap 允许为null 允许为null
Hashtable 不允许为null 不允许为null
Hashtable 不允许为null 不允许为null
LinkedHashMap 允许为null 不允许为null
ConcurrentHashMap 不允许为null 不允许为null
ConcurrentSkipListMap 不允许为null 不允许为null
**/
//List< String>转Map<String, List< Integer>>
Map<String, List<Integer>> map = list.stream().collect(Collectors.toMap(Function.identity(), s-> new ArrayList<>()));
一个list集合内有name,sex字段,需要判断list中是否有name有叫张三的人,如果有返回true
list.stream().filter(m->m.getName().equals("张三")).findAny().isPresent();
浙公网安备 33010602011771号