Stream(二)
程序员文章站
2022-05-31 10:06:56
...
import java.time.LocalDate; import java.util.Arrays; import java.util.IntSummaryStatistics; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; import org.junit.Test; import pojo.Employee; public class StreamTerminalTest { Employee[] e = { new Employee("张三", 500.0, 25, LocalDate.of(1995, 03, 07)), new Employee("李四", 400.0, 20, LocalDate.of(1999, 01, 13)), new Employee("王五", 700.0, 23, LocalDate.of(1997, 11, 27)), new Employee("赵六", 900.0, 26, LocalDate.of(1994, 07, 06)), new Employee("阿七", 400.0, 22, LocalDate.of(1998, 10, 19)), new Employee("阿七", 400.0, 22, LocalDate.of(1998, 10, 19)) }; // 终端操作(不返回Stream类的对象) // long count() 获取流的长度 @Test public void countTest() { List<Employee> list = Arrays.asList(e); long count = list.stream().count(); System.out.println(count); } 输出: 6 /* * Optional<T> max(Comparator<? super T> comparator) * 根据Comparator指定条件获取流中最大的元素的Optional,所谓的最大值就是重新进行排序的, max就是取重新排序后的最后一个元素的Optional */ @Test public void MaxTest() { List<Employee> list = Arrays.asList(e); //年龄最大的 Optional<Employee> max = list.stream().max((x, y) -> x.getAge().compareTo(y.getAge())); System.out.println(max.get()); } 输出: Employee [name=赵六, salary=900.0, age=26, hireDay=1994-07-06] /* * Optional<T> min(Comparator<? super T> comparator) * 根据Compator指定条件获取流中最小的元素的Optional,所谓的最小值就是重新进行排序的, min就是取重新排序后的第一个元素的Optional */ @Test public void MinTest() { List<Employee> list = Arrays.asList(e); Optional<Employee> min = list.stream().min((x, y) -> x.getAge().compareTo(y.getAge())); System.out.println(min.get()); } 输出: Employee [name=李四, salary=400.0, age=20, hireDay=1999-01-13] /* * boolean allMatch(Predicate<? super T> predicate) * 判断流中的元素是否都满足指定的条件,全部满足返回true,否则返回false */ @Test public void allMatchTest() { List<Employee> list = Arrays.asList(e); boolean allMatch = list.stream().allMatch((e) -> e.getAge() == 20); System.out.println(allMatch); } 输出: false /* * boolean anyMatch(Predicate<? super T> predicate) * 判断流中的元素是否都满足指定的条件,至少有一条满足返回true,否则返回false boolean */ @Test public void anyMatchTest() { List<Employee> list = Arrays.asList(e); boolean allMatch = list.stream().anyMatch((e) -> e.getAge() == 20); System.out.println(allMatch); } 输出: true /* * noneMatch(Predicate<? super T> predicate) * 判断流中的元素是否都满足指定的条件,如果所有元素都不满足条件,返回true;否则返回false. */ @Test public void noneMatchTest() { List<Employee> list = Arrays.asList(e); boolean allMatch = list.stream().noneMatch((e) -> e.getAge() < 20); System.out.println(allMatch); } 输出: true // Optional<T> findFirst() 返回流中的第一个元素的Optional,如果流为空,则返回一个空的Optional。 @Test public void findFirstTest() { List<Employee> list = Arrays.asList(e); Optional<Employee> first = list.stream().findFirst(); System.out.println(first); } 输出: Optional[Employee [name=张三, salary=500.0, age=25, hireDay=1995-03-07]] /* * Optional<T> findAny() 返回流中的某个元素的Optional,如果流为空,则返回一个空的Optional。 * 串行流中返回第一个元素,并行流中会*的选择流中的任意一个元素。 */ @Test public void findAnyTest() { List<Employee> list = Arrays.asList(e); Optional<Employee> first = list.stream().findAny(); System.out.println(first); } 输出: Optional[Employee [name=张三, salary=500.0, age=25, hireDay=1995-03-07]] // forEach(Consumer<? super T> action) 遍历流中的所有元素 @Test public void forEachTest() { List<Employee> list = Arrays.asList(e); Stream stream = list.stream(); stream.forEach(System.out::println); } 输出: Employee [name=张三, salary=500.0, age=25, hireDay=1995-03-07] Employee [name=李四, salary=400.0, age=20, hireDay=1999-01-13] Employee [name=王五, salary=700.0, age=23, hireDay=1997-11-27] Employee [name=赵六, salary=900.0, age=26, hireDay=1994-07-06] Employee [name=阿七, salary=400.0, age=22, hireDay=1998-10-19] Employee [name=阿七, salary=400.0, age=22, hireDay=1998-10-19] /* * forEachOrdered(Consumer<? super T> action) 遍历流中的所有元素 * 不同与forEach()的是,在使用并行流时,仍然按照初始流时元素的顺序遍历。 */ @Test public void forEachOrderedTest() { List<Employee> list = Arrays.asList(e); list.stream().forEachOrdered(System.out::println); } 输出: Employee [name=张三, salary=500.0, age=25, hireDay=1995-03-07] Employee [name=李四, salary=400.0, age=20, hireDay=1999-01-13] Employee [name=王五, salary=700.0, age=23, hireDay=1997-11-27] Employee [name=赵六, salary=900.0, age=26, hireDay=1994-07-06] Employee [name=阿七, salary=400.0, age=22, hireDay=1998-10-19] Employee [name=阿七, salary=400.0, age=22, hireDay=1998-10-19] /* * Optional<T> reduce(BinaryOperator<T> accumulator) * <U> U reduce(T identity,BinaryOperator<T> accumulator) * <U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner); * 将流中的元素反复结合起来,获得一个值 */ @Test public void reduceTest1() { List<Employee> list = Arrays.asList(e); //最大薪资 Optional<Employee> reduce = list.stream().reduce((x, y) -> { System.out.println("x:" + x.getName() + "y:" + y.getName()); return x.getSalary().compareTo(y.getSalary()) > 0 ? x : y; }); System.out.println(reduce.get()); } 输出: x:张三y:李四 x:张三y:王五 x:王五y:赵六 x:赵六y:阿七 x:赵六y:阿七 Employee [name=赵六, salary=900.0, age=26, hireDay=1994-07-06] @Test public void reduceTest2() { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); // 将0传进x与y(流中的每个元素)进行指定的操作(求总和) Integer reduce = list.stream().reduce(0, (x, y) -> { System.out.println("x:" + x + "y:" + y); return x + y; }); System.out.println(reduce); } 输出: x:1y:2 x:3y:3 x:6y:4 x:10y:5 15 @Test public void reduceTest3() { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); // 串行流中第三个参数不起作用 Integer reduce = list.stream().reduce(0, (x, y) -> { System.out.println("BiFunction x:" + x + "y:" + y); return x + y; }, (x, y) -> { System.out.println("BinaryOperator x:" + x + "y:" + y); return x + y; }); System.out.println(reduce); } 输出: BiFunction x:0y:1 BiFunction x:1y:2 BiFunction x:3y:3 BiFunction x:6y:4 BiFunction x:10y:5 15 //Object[] toArray() 将流转化为数组 @Test public void toArrayTest() { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); Object[] array = list.stream().toArray(); for (Object object : array) { System.out.println(object); } } 输出: 1 2 3 4 5 /* * <R, A> R collect(Collector<? super T, A, R> collector) * 收集器操作,收集器操作,可以当做是一种更高级的归约操作 */ @Test public void collectionTest() { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); List<Integer> collectList = list.stream().collect(Collectors.toList()); System.out.println("list:"+collectList); Map<Integer, Integer> map = list.stream().collect(Collectors.toMap(Integer::new, Integer::new)); System.out.println("map:"+map); System.out.println("==================="); // 平均值 Double avg = list.stream().collect(Collectors.averagingInt(Integer::new)); System.out.println("平均值:" + avg); // 最大值 Optional<Integer> max = list.stream().collect(Collectors.maxBy(Integer::compareTo)); System.out.println("最大值:" + max.get()); // 总结统计 IntSummaryStatistics intSummary = list.stream().collect(Collectors.summarizingInt(Integer::new)); System.out.println("最大值:" + intSummary.getMax()); System.out.println("最小值:" + intSummary.getMin()); System.out.println("总值:" + intSummary.getAverage()); System.out.println("平均值:" + intSummary.getSum()); System.out.println("总数:" + intSummary.getCount()); } 输出: list:[1, 2, 3, 4, 5] map:{1=1, 2=2, 3=3, 4=4, 5=5} =================== 平均值:3.0 最大值:5 最大值:5 最小值:1 总值:3.0 平均值:15 总数:5 }
上一篇: 人人网推“移动安全支付”,为开发者提供新的支付渠道
下一篇: phalcon数据库DB使用实例