/**
* 获取年龄是22岁的员工
*/
@Test
public void testFilter() {
list.stream()
.filter(employee -> employee.getAge() == 22)
.forEach(System.out::println);
}
/**
* 获取所有员工的姓名,并用","连接
*/
@Test
public void testMap() {
String str = list.stream()
.map(Employee::getName)
.collect(Collectors.joining(","));
System.out.println(str);
}
/**
* 按照工资倒序排列,并获取第一个
*/
@Test
public void testSort() {
Optional<Employee> optional = list.stream().sorted((e1, e2) -> {
if (e1.getSlary() > e2.getSlary()) {
return -1;
} else if (e1.getSlary() == e2.getSlary()) {
return 0;
} else {
return 1;
}
}).findFirst();
System.out.println(optional.get());
}
/**
* 最少工资
*/
@Test
public void testMin() {
Double slary = list.stream()
.map(Employee::getSlary)
.min(Double::compareTo)
.get();
System.out.println(slary);
}
/**
* 根据年龄进行分组
*/
@Test
public void testGroup() {
Map<Integer, List<Employee>> map = list.stream().collect(Collectors.groupingBy(Employee::getAge));
System.out.println(map);
}
/**
* 工资大于5000的个数
*/
@Test
public void testCount() {
Long count = list.stream().filter(employee -> employee.getSlary() > 8000)
.count();
System.out.println(count);
}
@Test
public void testA() {
BiPredicate<String, String> predicate2 = (s1, s2) -> s1.equals(s2);
BiPredicate<String, String> predicate = String::equals;
BiConsumer<List, String> bf = List::add;
Function<Employee, String> f = Employee::getName;
Supplier<String> s = String::new;
}
/**
* 总薪水
*/
@Test
public void testStatistic() {
DoubleSummaryStatistics statistics = list.stream().collect(Collectors.summarizingDouble(Employee::getSlary));
System.out.println(statistics.getSum());
}