• Java8之Stream


    在使用Stream之前先介绍几个概念:1、函数式接口 2、常用的函数式接口 3、Lambda 4、方法引用 

    1、函数式接口

      1.只定义了一个抽象方法的接口。
      2.JDK8接口中的静态方法和默认方法,都不算是抽象方法。
      3.接口默认继承java.lang.Object,所以如果接口显示声明覆盖了Object中方法,那么也不算抽象方法。

    @FunctionalInterface
    public interface Runnable {
      public abstract void run();
    }

    2、常用的函数式接口

      1.Predicate<T>
      接口接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将Predicate组合成其他复杂的逻辑(比如:与,或,非)。可以用于接口请求参数校验、判断新老数据是否有变化需要进行更新操作。
      boolean test(T t);  例:Predicate<String> p = s -> s.equals(“hello”);  p.test(“hello”);  

      2.Consumer<T>接口接受一个T类型参数,没有返回值。

      void accept(T t);  例:Consumer<Integer> c = i-> System.out.println(i);  Stream.of(1,2,3,4).forEach(c);

      3.Supplier<T> 接口没有入参,返回一个T类型的对象,类似工厂方法。 T get();

      4.Function<T,R > 对类型为T的对象应用操作,并返回结果,结果是R类型的对象  R apply(T t);

      5.UnaryOperator<T > extends Function<T, T> 对类型为T的对象应用操作,并返回结果,结果是T类型的对象   T apply(T t);

      6.BiFunction<T,U,R> 接口对类型为T和U的对象应用操作,返回类型为R的对象。 R apply(T t, U u);

      7.BinaryOperator<T > extends BiFunction<T,T,T> 接口对两个类型为T的对象应用操作,返回类型为T的对象。T apply(T t, T t);

      8.Comparator<T>比较两个类型为T的对象,返回int类型。int compare(T o1, T o2);

     3、Lambda

    public static void main(String[] args) {
      int c = 10;
      MathOperation m = (a,b)->a+b+c;//编译报错
      c = 1;
    }
    interface MathOperation {
      int operation(int a, int b);
    }

    4、方法引用

    格式:<Class or Instance>::<Method>

    四种不同类型的方法引用:

      引用静态方法 ContainingClass::staticMethodName 例: String::valueOf,对应的Lambda:(s) -> String.valueOf(s) 

      引用特定对象的实例方法 containingObject::instanceMethodName 例子: x::toString,对应的Lambda:() -> this.toString()

      引用特定类型的任意对象的实例方法 ContainingType::methodName 例子: String::toString,对应的Lambda:(s) -> s.toString() 

      引用构造函数 ClassName::new 例子: String::new,对应的Lambda:() -> new String() 

    5、Optional类

    Optional 类主要解决的问题是空指针异常,它本质上是包含有可选值的包装类,可以含有对象也可以为空
    常用方法:
      isPresent() – 如果有值则返回true
      get() – 如果值存在则返回值
      orElse(T other) –如果值存在则返回值,否则返回other
      ifPresent(Consumer<? super T> consumer) – 如果值存在,则执行consumer

    Optional<String> optional = Optional.of("hello");
    if (optional.isPresent()){
        String hello = optional.get();
    }
    String world = optional.orElse("world");

    6、Stream

    Stream的工作方式 https://www.ibm.com/developerworks/cn/java/j-java-streams-3-brian-goetz/fuse.svg,Stream的中间参数一般为常用函数式接口,因此理解函数式接口之后就能很好的使用Stream。

     Arrays.asList("hello", "world", "hello world").stream().mapToInt(item -> {  
      int length = item.length();
      System.out.println(item);
      return length; }).filter(length -> length == 5).findFirst().ifPresent(System.out::println);

       1.Stream的创建

    //从单个元素创建 
    Stream.of(val1, val2, …)
    //从集合创建
    someList.stream()
    //从数组创建  
    Stream.of(someArray)
    Arrays.stream(someArray)
    //创建无限流
    IntStream.range(0,100)
    IntStream.iterate(0, i -> i + 1)

      2.中间操作

    Stream.of(“abc”, “”, “bc”, “efg”).filter(string - > !string.isEmpty()).collect(Collectors.toList());
    
    Arrays.asList(3, 2, 2, 3, 7, 3, 5).stream().map(i-> i*i).distinct().collect(Collectors.toList());
    
    IntStream.range(0,100).limit(10).forEach(System.out::println); 
    
    Stream.of(1,2,3,4,5,6).peek(n-> System.out.println(n*2)).map(n->n+"h").forEach(System.out::println) ;
    
    Stream.of(5,8,7,3,4,1,9).sorted(Integer::compare).forEach(System.out::println);Stream.of(5,8,7,3,4,1,9).sorted((a,b)->a-b).forEach(System.out::println);
    
    students.stream().sorted(Comparator.comparingInt(Student::getAge).reversed().thenComparing(Student::getName)).forEach(System.out::println);
    
    List<String> list1 = Arrays.asList("a","b","c");
    List<String> list2 = Arrays.asList("d","e","c");
    List<String> list3 = Arrays.asList("f","g","c");
    List<List<String>> list = Arrays.asList(list1,list2,list3);
    List<String> result = list.stream().flatMap(stringList->stringList.stream()).distinct().collect(Collectors.toList())

      3.终端操作

    Stream.of(5,8,7,3,4,1,9).min(Integer::compareTo).get() ;
    Stream.of(5,8,7,3,4,1,9).max(Integer::compareTo).get() ;
    Stream.of(5,8,7,3,4,1,9).anyMatch(num->num==9);
    Stream.of(5,8,7,3,4,1,9).allMatch(num->num==9);
    Stream.of(5,8,7,3,4,1,9).count();
    Stream.of(5,8,7,3,4,1,9).findAny().get();
    Stream.of(5,8,7,3,4,1,9).findFirst().get();
    /*Reduce:从一个作为累加器的初始值开始,利用binaryOperator与流中的元素逐个结合,从而将流归约为单个值 。reduce操作每处理一个元素总是创建一个新值*/
    Stream.of(5,8,7,3,4,1,9).reduce(1,(sum,item)->sum+item);
    Stream.of(5,8,7,3,4,1,9).reduce(Integer::sum).get();
    /*Collect:接收的参数是将流中的元素累积到汇总结果的各种方式(称为收集器)。修改现存的值,而不是每处理一个元素,创建一个新值*/
    students.stream().map(student -> student.getName()).collect(Collectors.toList());
    students.stream().map(student -> student.getName()).collect(Collectors.toSet());
    students.stream().collect(Collectors.toMap(s1 -> s1.getName(), s2 -> s2.getAge()));
    Map<String, List<Student>> map = students.stream().collect(Collectors.groupingBy(Student::getName));

      4.一个比较好用的case

    //多线程处理有序的集合
    IntStream.range(0, files.size()).parallel().forEach(fileIndex -> {do something about files});
  • 相关阅读:
    SORT ORDER BY STOPKEY
    javaScript 数值型和字符串型之间的转换
    Caused by: org.apache.ibatis.type.TypeException: Could not resolve type alias 'Student'
    Caused by: java.sql.SQLException: Field 'stu_id' doesn't have a default value
    Result Maps collection does not contain value for StudentMapper.StudentMap
    集群维护
    logstash 调用exec
    logstash zabbix插件
    logstash 调用脚本告警
    Caused by: java.lang.NoSuchMethodException: com.you.entity.sys.Param.()
  • 原文地址:https://www.cnblogs.com/rayallenbj/p/8466066.html
Copyright © 2020-2023  润新知