常用API
List<Employee> emps = Arrays.asList(
new Employee(101, "张三", 18, 9999.99),
new Employee(102, "李四", 59, 6666.66),
new Employee(103, "王五", 28, 3333.33),
new Employee(104, "赵六", 8, 7777.77),
new Employee(105, "田七", 38, 5555.55),
new Employee(105, "田七", 38, 5555.55)
);
List<String> stringList = Arrays.asList("aaa", "bbb", "ccc");
List<Employee> list = emps.stream().filter(e -> e.getAge() > 35).collect(Collectors.toList());
List<Employee> list1 = emps.stream().limit(3).collect(Collectors.toList());
List<Employee> list2 = emps.stream().skip(2).collect(Collectors.toList());
List<Employee> list3 = emps.stream().distinct().collect(Collectors.toList());
List<String> list4 = stringList.stream().map(str -> str.toUpperCase()).collect(Collectors.toList());
List<String> list5 = stringList.stream().map(String::toUpperCase).collect(Collectors.toList());
List<String> list6 = emps.stream().map(emp -> emp.getName()).collect(Collectors.toList());
List<String> list7 = emps.stream().map(Employee::getName).collect(Collectors.toList());
List<String> list8 = stringList.stream().sorted().collect(Collectors.toList());
List<Employee> list9 = emps.stream().sorted((x, y) -> {
if (x.getAge() == y.getAge()) {
return x.getName().compareTo(y.getName());
} else {
return x.getAge() - y.getAge();
}
}).collect(Collectors.toList());
boolean b = emps.stream().anyMatch(x -> x.getName().equals("张三"));
Optional<Employee> emp = emps.stream().max((x, y) -> (int) (x.getSalary() - y.getSalary()));
Optional<Double> max = emps.stream().map(s -> s.getSalary()).max(Double::compare);
System.out.println(max.get() == 9999.99);
Optional<Double> sumSal = emps.stream().map(x -> x.getSalary()).reduce(Double::sum);
System.out.println(sumSal.get());
Set<String> set = emps.stream().map(s -> s.getName()).collect(Collectors.toSet());
Double avgSal = emps.stream().collect(Collectors.averagingDouble(x -> x.getSalary()));
Map<Integer, List<Employee>> collect = emps.stream().collect(Collectors.groupingBy(x -> x.getAge()));
Map<Integer, Map<String, List<Employee>>> integerMapMap = emps.stream().collect(Collectors.groupingBy(x -> x.getAge(), Collectors.groupingBy(y -> {
if (y.getSalary() <= 5000) {
return "低工资";
} else {
return "高工资";
}
})));
Map<String, List<DfsGoodsMappingEntity>> groupMap = entityList
.stream()
.collect(groupingBy(DfsGoodsMappingEntity::getUpc,LinkedHashMap::new,Collectors.toList()));
Map<Object, List<Map<String, Object>>> listMap = maps
.stream()
.collect(Collectors.groupingBy(x -> x.get("typeOfId"), LinkedHashMap::new, Collectors.toList()));
Map<Boolean, List<Employee>> map = emps.stream().collect(Collectors.partitioningBy(x -> x.getSalary() > 8000));
List<Employee> trueList = map.get(true);
List<Employee> falseList = map.get(false);