一、包装数据类型
List<Integer> list = new ArrayList<Integer>(){{
add(7); add(5); add(1); add(2)
}};
List<Integer> filterList = list.stream().filter(a -> a < 5).collect(Collectors.toList());
List<Integer> sortList1 = list.stream().sorted().collect(Collectors.toList());
List<Integer> sortList2 = list.stream().sorted(Comparator.comparing(a -> a, Comparator.naturalOrder())).collect(Collectors.toList());
List<Integer> sortList3 = list.stream().sorted(Comparator.comparing(a -> a, Comparator.reverseOrder())).collect(Collectors.toList());
Integer max = list.stream().max(Comparator.comparing(a -> a, Comparator.naturalOrder())).get();
Integer min = list.stream().min(Comparator.comparing(a -> a, Comparator.naturalOrder())).get();
List<Integer> distinctList = list.stream().distinct().collect(Collectors.toList());
List<Integer> mapList = list.stream().map(a -> a * a).collect(Collectors.toList());
二、Map类型
HashMap<String, Integer> map = new HashMap<String, Integer>(){{
put("A", 1); put("B", 2); put("C", 3); put("D", 4)
}};
List<String> keyList1 = map.keySet().stream().filter(a -> StringUtils.isNotBlank(a)).collect(Collectors.toList());
List<String> keyList2 = map.entrySet().stream().map(entry -> entry.getKey()).filter(a -> StringUtils.isNotBlank(a)).collect(Collectors.toList());
List<Integer> valueList = map.entrySet().stream().map(entry -> entry.getValue()).collect(Collectors.toList());
Map<String, Integer> newMap = map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, a -> a.getValue() * a.getValue()));
Map<Integer, String> reMap1 = map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue,
Map.Entry::getKey,
(String val1, String val2) -> val2
));
Map<Integer, List<String>> reMap2 = map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue,
a -> new ArrayList<String>(){{ add(a.getKey()); }},
(List<String> v1, List<String> v2) -> {
v1.addAll(v2);
return v1;
}
));
三、对象List
List<User> list = new ArrayList<User>() {{
add(new User(1, "老大", 18));
add(new User(2, "老二", 22));
add(new User(3, "老三", 24));
add(new User(3, "老四", 28));
add(new User(4, "老五", 28));
}};
Map<Integer, User> map1 = list.stream().collect(Collectors.toMap(User::getId, Function.identity()));
Map<Integer, User> map2 = list.stream().collect(Collectors.toMap(User::getId, a -> a));
User user = list.stream().filter(a -> "半月".equals(a.getName())).findFirst().get();
long count = list.stream().filter(a -> a.getAge() >= 22).count();
List<User> orderList = list.stream().sorted(Comparator.comparing(User::getAge, Comparator.naturalOrder())).collect(Collectors.toList());
List<User> reorderList = list.stream().sorted(Comparator.comparing(User::getAge, Comparator.reverseOrder())).collect(Collectors.toList());
List<User> unionUser = list.stream().collect(
Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getId()))), ArrayList::new));
List<String> nameList = list.stream().map(User::getName).collect(Collectors.toList());
List<User> limitList = list.stream().skip(1).limit(2).collect(Collectors.toList());
boolean isHave1 = list.stream().anyMatch(a -> "半月".equals(a.getName()));
boolean fullAge = list.stream().allMatch(a -> a.getAge() > 16);
boolean isHave2 = list.stream().noneMatch(a -> "10".equals(a.getId()));
}