(5.3.1)创建Stream方式一: 通过集合
List<Employee> employees = EmployeeData.getEmployees(); // default Stream<E> stream(): 返回一个顺序流 Stream<Employee> stream = employees.stream(); // parallelStream(): 返回一个并行流 Stream<Employee> parallelStream = employees.parallelStream();
(5.3.2)创建Stream方式二: 通过数组
// 调用Arrays类的 static <T> Stream<T> stream(T[] array): 返回一个流 int[] arr = new int[]{1,2,3,45,6}; IntStream stream2 = Arrays.stream(arr); Employee e1 = new Employee(1,"Tom"); Employee e2 = new Employee(2,"Jack"); Employee[] emp = new Employee[]{e1,e2}; Stream<Employee> stream3 = Arrays.stream(emp);
(5.3.3)创建Stream方式三: 通过Stream的of()
Stream<Integer> stream4 = Stream.of(1,2,3,4,5,6);
Stream<Employee> stream5 = Stream.of(e1,e2);
(5.3.4)创建Stream方式四: 创建无限流
// 迭代:public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f) Stream.iterate(0, t -> t+2).forEach(System.out::println); // 遍历前10个偶数 Stream.iterate(0, t -> t+2).limit(10).forEach(System.out::println); // 生成: public static<T> generate(Supplier<T> s) Stream.generate(Math::random).limit(10).forEach(System.out::println);
List<Employee> employees = EmployeeData.getEmployees(); Stream<Employee> stream = employees.stream(); stream.filter(e -> e.getSalary() > 7000).forEach(System.out::println); stream.limit(3).forEach(System.out::println); stream.skip(3).forEach(System.out::println); stream.distinct().forEach(System.out::println);
(5.4.2)映射
List<String> list = Arrays.asList("aa","vv","cc","dd"); list.stream().map(str -> str.toUpperCase()).forEach(System.out::println); List<Employee> employees = EmployeeData.getEmployees(); Stream<Employee> stream = employees.stream(); Stream<String> nameStream = stream.map(Employee::getName); nameStream.filter(name -> name.length() > 3).forEach(System.out::println); Stream<Stream<Character>> streamStream = list.stream().map(StreamTest::fromStringToStream); streamStream.forEach(s -> { s.forEach(System.out::println); }); Stream<Character> characterStream = list.stream().flatMap(StreamTest::fromStringToStream); characterStream.forEach(System.out::println);
public static Stream<Character> fromStringToStream(String str){ ArrayList<Character> list = new ArrayList<>(); for(Character c: str.toCharArray()){ list.add(c); } return list.stream(); }
(5.4.3)排序
// sorted():自然排序 List<Integer> list = Arrays.asList(12,43,65,34,87,1,-9,19); list.stream().sorted().forEach(System.out::println); // sorted(Comparator com): 定制排序 List<Employee> employees = EmployeeData.getEmployees(); employees.stream().sorted((e1,e2) ->{return Integer.compare(e1.getAge(), e2.getAge());}).forEach(System.out::println); employees.stream().sorted((e1,e2) ->{ int ageValue = Integer.compare(e1.getAge(), e2.getAge()); if(ageValue != 0){ return ageValue; }else{ return Double.compare(e1.getSalary(), e2.getSalary()); } }).forEach(System.out::println);
(5.5.1)匹配与查找
// 是否所有的员工年龄都大于18 boolean allMatch = employees.stream().allMatch(e -> e.getAge() > 18); System.out.println(allMatch); // 是否有任意一个员工的工资大于10000 boolean anyMatch = employees.stream().anyMatch(e -> e.getSalary() > 10000); System.out.println(anyMatch); // 是否所有员工都不姓"雷", boolean noneMatch = employees.stream().noneMatch(e -> e.getName().startsWith("雷")); System.out.println(noneMatch); // 返回第一个元素 Optional<Employee> firstEmp = employees.stream().findFirst(); System.out.println(firstEmp); // 返回任意元素 Optional<Employee> anyEmp = employees.parallelStream().findAny(); System.out.println(anyEmp);
// 返回最大值 Stream<Double> salaryStream = employees.stream().map(Employee::getSalary); Optional<Double> maxSalary = salaryStream.max(Double::compare); System.out.println(maxSalary.get()); //Optional[9865.38] 9865.38 // 返回最小值 Optional<Employee> minSalaryEmp = employees.stream().min((e1,e2) -> Double.compare(e1.getSalary(),e2.getSalary())); System.out.println(minSalaryEmp); //Optional[Employee{id=1008, name=‘扎克伯格‘, age=35, salary=2500.38}] // forEach(Consumer c): 内部迭代 employees.stream().forEach(System.out::println);
(5.5.2)归约
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10); int sum = list.stream().reduce(0,Integer::sum); System.out.println(sum); // 55 List<Employee> employees = EmployeeData.getEmployees(); Optional<Double> salarySum = employees.stream().map(e -> e.getSalary()).reduce(Double::sum); System.out.println(salarySum); // Optional[48675.03999999999] Optional<Double> salarySum2 = employees.stream().map(Employee::getSalary).reduce((d1,d2) -> d1+d2); System.out.println(salarySum2); // Optional[48675.03999999999]
(5.5.2)收集
Collector 接口中方法的实现决定了如何对流执行收集的操作(如收集到List、Set、Map)。
另外,Collectors实用类提供了很多静态方法,可以创建常见收集器实例。
方法 | 返回类型 | 作用 |
toList | List<T> | 把流中元素收集到List |
toSet | Set<T> | 把流中元素收集到Set |
toCollection | Collection<T> | 把流中元素收集到创建的集合 |
counting | Long | 计算流中元素的个数 |
summingInt | Integer | 对流中元素的整数属性求和 |
avaragingInt | Double | 计算流中元素Integer属性的平均值 |
summarizingInt | IntSummaryStatistics | 收集流中Integer属性的统计值。如平均值、最大值、最小值、总数量 |
List<Employee> employees = EmployeeData.getEmployees(); List<Employee> empList = employees.stream().filter(e -> e.getSalary() > 6000).collect(Collectors.toList()); empList.forEach(System.out::println); Set<Employee> empSet= employees.stream().filter(e -> e.getSalary() > 6000).collect(Collectors.toSet()); empSet.forEach(System.out::println);
Collection<Employee> emps = employees.stream().collect(Collectors.toCollection(ArrayList::new)); System.out.println(emps); long count = employees.stream().collect(Collectors.counting()); System.out.println(count); // 8 double totalSalary = employees.stream().collect(Collectors.summingDouble(Employee::getSalary)); System.out.println(totalSalary); //48675.04 double avgSalary = employees.stream().collect(Collectors.averagingDouble(Employee::getSalary)); System.out.println(avgSalary); //6084.38 DoubleSummaryStatistics dss = employees.stream().collect(Collectors.summarizingDouble(Employee::getSalary)); System.out.println(dss.getMax() + "--" + dss.getMin() + "--" + dss.getAverage()); //9865.38--2500.38--6084.38
(6)Optional类
(6.1)Optional类方法
Girl girl = new Girl(); girl = null; // Optional<Girl> optionalGirl = Optional.of(girl); // java.lang.NullPointerException Girl girl2 = new Girl(); girl2 = null; Optional<Girl> optionalGirl2 = Optional.ofNullable(girl2); Girl girl3 = optionalGirl2.orElse(new Girl("春天",11)); System.out.println(girl3); // Girl{name=‘null‘, age=0, boy=null} 未添加 girl2 =null时 // Girl{name=‘春天‘, age=11, boy=null}
未做判断时: 报空指针异常
public String getGirlName(Boy boy){ return boy.getGirl().getName(); } @Test public void test2(){ Boy boy = new Boy(); String girlName = getGirlName(boy); // java.lang.NullPointerException System.out.println(girlName); }
做判断: 返回null
public String getGirlName(Boy boy){ // return boy.getGirl().getName(); if(boy != null){ Girl girl = boy.getGirl(); if(girl != null){ return girl.getName(); } } return null; }
使用Optional 实现
public String getGirlName2(Boy boy){ Optional<Boy> boyOptional = Optional.ofNullable(boy); Boy boy2 = boyOptional.orElse(new Boy(new Girl("地理",23))); Girl girl = boy2.getGirl(); Optional<Girl> girlOptional = Optional.ofNullable(girl); Girl girl2 = girlOptional.orElse(new Girl("古老",12)); return girl2.getName(); }
// boy=null时 boy2将Girl进行赋值; boy=new Boy()时 boy非空,到了 girl2时对Girl进行赋值 @Test public void test3(){ Boy boy = null; boy = new Boy(); String girlName = getGirlName2(boy); System.out.println(girlName); // boy=null: 地理 boy=new Boy():古老 }
Optional<Object> op1 = Optional.empty(); if(!op1.isPresent()){ System.out.println("数据为空"); // 数据为空 } System.out.println(op1); // Optional.empty System.out.println(op1.isPresent()); // false System.out.println(op1.get()); // java.util.NoSuchElementException: No value present
String str = "hello"; str = null; // of()要求t非空,如t为空,get()方法报错 Optional<String> op2 = Optional.of(str); System.out.println(op2.get()); // hello 加入str=null: java.lang.NullPointerException
String str2 = "beijn"; str2 = null; Optional<String> op3 = Optional.ofNullable(str2); String str3 = op3.orElse("shanho"); System.out.println(str3); // beijn 加入str2=null: shanho
原文:https://www.cnblogs.com/kingdomer/p/14116455.html