java jdk1.8新特性Stream API( 四 )




/**
* 查找与匹配 allMatch —— 查抄是否平匹配所有元素 anyMatch —— 查抄是否至少匹配一个元素 noneMatch ——
* 查抄是否没有匹配的元素 findFirst —— 返回第一个元素 findAny —— 返回当前流中的肆意元素 count —— 返回流中元素的总个数
* max —— 返回流中最大值 min —— 返回流中最小值
*/
@Test
public void test1() {


boolean b1 = employees.stream().allMatch(e -> e.getStatus().equals(Status.BUSY));


System.out.println(b1);


boolean b2 = employees.stream().anyMatch((e) -> e.getStatus().equals(Status.BUSY));


System.out.println(b2);


boolean b3 = employees.stream().noneMatch((e) -> e.getStatus().equals(Status.BUSY));


System.out.println(b3);


Optional<Employee> op1 = employees.stream().sorted((e1, e2) -> -Double.compare(e1.getSalary(), e2.getSalary()))
.findFirst();


System.out.println(op1.get());


Optional<Employee> op2 = employees.stream().filter((e) -> e.getStatus().equals(Status.FREE)).findAny();


System.out.println(op2.get());


Optional<Employee> op3 = employees.parallelStream().filter((e) -> e.getStatus().equals(Status.FREE)).findAny();


System.out.println(op3.get());


}


@Test
public void test2() {
Long count = employees.stream().count();


System.out.println(count);


Optional<Employee> op1 = employees.stream().max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));


System.out.println(op1.get());


Optional<Double> op2 = employees.stream().map(Employee::getSalary).max(Double::compare);


System.out.println(op2.get());
}


}

java jdk1.8新特性Stream API

文章插图

java jdk1.8新特性Stream API

文章插图

66、Stream约与收集归
package com.stream.api6;


import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;


import org.junit.Test;


import com.stream.api1.Employee;
import com.stream.api1.Employee.Status;


public class TestStreamAPI6 {


List<Employee> employees = Arrays.asList(new Employee("张三", 12, 1200.99, Status.BUSY),
new Employee("小明", 15, 4500.99, Status.BUSY), new Employee("小丽", 16, 5500.99, Status.BUSY),
new Employee("王二", 32, 1100.99, Status.FREE), new Employee("二虎", 22, 9825.99, Status.FREE),
new Employee("李静", 18, 4502.99, Status.FREE), new Employee("小三", 17, 1469.99, Status.VOCATION),
new Employee("小三", 17, 1469.99, Status.VOCATION), new Employee("小三", 17, 1469.99, Status.VOCATION),
new Employee("小三", 17, 1469.99, Status.VOCATION));


/**
* 归约 reduce(T identity,BinaryOperator)/reduce(BinaryOperator) ——
* 可以将流中元素频频连系起来 , 获得一个值
*/


@Test
public void test1() {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

推荐阅读