# java8andDisruptor **Repository Path**: yjlfish2/java8andDisruptor ## Basic Information - **Project Name**: java8andDisruptor - **Description**: 基于实践经验总结的java8 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2018-06-18 - **Last Updated**: 2024-05-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # java8andDisruptor #### 项目介绍 基于实践经验总结的java8 #### 软件架构 软件架构说明 # 00-实际应用中的java8-lambda引言 ### 从孔乙己想到的:孔乙己知道茴香的茴字有多少种写法,一般人觉得没啥用,但是程序员会把各种茴字应用到不同的地方,所以程序员要知道茴字的n种写法。 我们知道java8主推的是函数式编程 函数是java中的一等公民 概念不细说,只说应用上的 那么函数式的写法有几种呢? 函数格式 一、完整写法 1针对有返回值 (参数类型 参数名)-\>{return 值} 例子:(String a,String b)-\>{String c=a+b;return c;}; 2针对无返回值 (参数类型 参数名)-\>{处理逻辑} 例子:(String a,String b)-\>{System.out.println(a+b);}; 二、简略写法:【利用的是(类型推断)】 (a,b)-\>a+b; 【注意:两个参数以上必须加括号】 s-\>System.out.println(s) 三、还有一种简略写法叫做【方法引用】,这个是需要结合类名,this,super,以及具体方法名使用的 形式如 this::a ,super::b,A::a ### 函数式写法可以代替任何方法引用,反之不可以,但是方法引用很常见更简洁,合适的场景可以使用方法引用 那么方法引用怎么对应到函数式写法呢 比如有个类A,它有个方法 public static String a(String b){return “hello”;} 那么它的函数式写法就是(String b)-\>”hello" 或者 b-\>”hello” 对应的方法引用就是A::a ### 但是使用它们都是有场景的,接下来再说场景 函数签名 函数签名是根据函数式写法衍生出来的一种抽象脑图【自己的理解】 比如传参String 返回boolean 那么它的签名就是 string -\>boolean 没有参数就是 ()-\>int….. 再没有返回值就是()-void 【这是个什么呢,这是个Runnable,典型的没有返回值,只有处理逻辑】 ### 那么函数想要应用总得有类型吧?直接使用可以,但是我们要想间接使用函数,或者参数就是函数,返回值是另外一个函数的场景怎么做 函数的返回类型在java8中内置了很多类型还有自定义类型 我们先说一个自定义类型 组成 @FunctionalInterface public interface AA\ { String aa(String a); } @FunctionalInterface不是必须的,但是添加上可以让ide检查它是否符合函数规范 也就是函数类型必须是一个只可以包含一个抽象方法的类型,(但是可以包含若干静态方法和默认方法) ### 实际应用中我们很少自定义函数 ### java8内置的常用函数类型 在java8的java.util.function包中包含所有函数式接口 ![](resources/0340DFA974FA32AC8F87A8DC99089A54.jpg) ![](resources/C1289515EED16C0F97B1C35FF524DDC6.jpg) 1public interface Predicate\ 他有个boolean test(T t);从这个方法可以推断它的函数签名 T-\>boolean 可以用来过滤 2public interface Consumer\ void accept(T t); 从这个方法可以推断它的函数签名 T-\>void 可以用来处理 也就是作为消费者了 3public interface Function\ R apply(T t); 从这个方法可以推断它的函数签名 T-\>R 可以用来返回计算后的结果,也能作为生产者 4public interface Supplier\ T get();从这个方法可以推断它的函数签名 ()-\>T 作为生产者 5public interface BiFunction\ R apply(T t, U u); 从这个方法可以推断它的函数签名 (T,U)-\>R 无非就是两个传参,返回计算的值 6public interface BinaryOperator\ extends BiFunction\ 注意,它集成自BiFunction,他只有T apply(T t, T u);; 函数签名(T,T)-\>T 所以我们在实际应用中如果必须用到函数式编程 我们想到什么样的函数签名来解决某件事情 必须考虑传参和返回值 其他函数大家自己从java.util.function查看 下面我们做个练习 以下分别是个什么函数? (int,int)-\>int (String,String)-\>int ()-\>String int-\>int int-\>void ()-\>void 下边我们给出答案 以下分别是个什么函数? (int,int)-\>int 这是一个 BinaryOperator 也可以说是一个BiFunction (String,String)-\>int 这是一个BiFunction 也可一说是一个Comparator 因为Comparator也是个函数式类型 @FunctionalInterface public interface Comparator\ int compare(T o1, T o2); 它的函数式签名是(T,T)-\>int 也正好符合以上签名 ()-\>String 这是一个Supplier 也可以说是一个callable 为什么这么说呢 @FunctionalInterface public interface Callable\ V call() throws Exception; 它的函数式签名是()-\>V 也符合签名 int-\>int 是一个Function int-\>void 是一个Consumer ()-\>void 是一个Runnable 因为 @FunctionalInterface public interface Runnable public abstract void run() 下边我么简单来举几个函数例子 参见 LamdaTest0 ```java @Test public void predictTest0(){//lambda Predicate p1=(String s)->s.contains("1"); // Predicate p1=s->s.contains("1"); String words="sdsds1asa"; String words1="sdsdsasa"; System.out.println(p1.test(words)); System.out.println(p1.test(words1)); } //方法引用静态方法 public static boolean predict(String s){ return s.contains("1"); } @Test public void predictTest1(){//方法引用 Predicate p1=LamdaTest0::predict; // Predicate p1=s->s.contains("1"); String words="sdsds1asa"; String words1="sdsdsasa"; System.out.println(p1.test(words)); System.out.println(p1.test(words1)); } //过滤符合条件的列表 public static List filterList(List sourceList,Predicate p){ List targetList = new ArrayList<>(); for (E e:sourceList){ if (p.test(e)) { targetList.add(e); } } return targetList; } @Test public void predictTest2(){//方法引用 List result = filterList( Arrays.asList("sss1ss","aaaa","12345"), s -> s.contains("1") ); System.out.println(result); } @Test public void functionTest1(){//function测试 Function func = s -> s.length(); System.out.println(func.apply("asdf")); } @Test public void functionTest2(){//function组合 Function func1 = s -> s.length(); Function func2 = i->i*2; Function func3 = func1.andThen(func2); System.out.println(func3.apply("asdf")); } ```的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)