|
@@ -0,0 +1,120 @@
|
|
|
+package com.sf.lambda;
|
|
|
+
|
|
|
+import com.sf.lambda.impl.AddCommand;
|
|
|
+
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.function.Function;
|
|
|
+
|
|
|
+public class CommandTest {
|
|
|
+
|
|
|
+ // 命令模式
|
|
|
+ public static void main(String[] args) {
|
|
|
+ ProcessArray pa = new ProcessArray();
|
|
|
+ int[] arr = {8, 9, 2, 1};
|
|
|
+// pa.process(target,new PrintCommand());
|
|
|
+// pa.process(target,new AddCommand());
|
|
|
+
|
|
|
+ // 匿名内部类的使用 是因为不需要关心类的名字 只需要实现接口的处理逻辑
|
|
|
+// pa.process(arr, new Command() {
|
|
|
+// @Override
|
|
|
+// public void process(int[] target) {
|
|
|
+// System.out.println(Arrays.toString(target));
|
|
|
+// }
|
|
|
+// });
|
|
|
+ // lambda表达式 也不再关心方法的名字了 只保留方法的入参和方法的执行逻辑
|
|
|
+ pa.process(arr, (int[] target) -> {
|
|
|
+ System.out.println(Arrays.toString(target));
|
|
|
+ });
|
|
|
+
|
|
|
+ System.out.println("============");
|
|
|
+ // 无参数 无返回值
|
|
|
+ CommandSimple cs = new CommandSimple() {
|
|
|
+ @Override
|
|
|
+ public void process() {
|
|
|
+ System.out.println("CommandSimple");
|
|
|
+ }
|
|
|
+ };
|
|
|
+ cs.process();
|
|
|
+
|
|
|
+ CommandSimple cs1 = () -> {
|
|
|
+ System.out.println("CommandSimple from lambda");
|
|
|
+ };
|
|
|
+ cs1.process();
|
|
|
+
|
|
|
+ CommandSimple cs2 = () -> System.out.println("CommandSimple from lambda Simple");
|
|
|
+ cs2.process();
|
|
|
+
|
|
|
+ System.out.println("============");
|
|
|
+ // 一个参数 无返回值
|
|
|
+ CommandOneParam cop = new CommandOneParam() {
|
|
|
+ @Override
|
|
|
+ public void process(int x) {
|
|
|
+ System.out.println("x=" + x);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ CommandOneParam cop1 = (int x) -> {
|
|
|
+ System.out.println("x=" + x);
|
|
|
+ };
|
|
|
+ cop1.process(10);
|
|
|
+
|
|
|
+ CommandOneParam cop2 = (int x) -> System.out.println("x=" + x);
|
|
|
+ // 能够推断出类型的参数 连类型都可以简化
|
|
|
+ CommandOneParam cop3 = x -> System.out.println(x);
|
|
|
+ cop3.process(20);
|
|
|
+
|
|
|
+ // 是一种进一步对参数的简化 是谁调用了(::)什么方法
|
|
|
+ CommandOneParam cop4 = System.out::println;
|
|
|
+ cop4.process(100);
|
|
|
+
|
|
|
+ // 比较的逻辑 如果第一个长度小 返回-1 如果第一个长度大 返回1 否则返回0
|
|
|
+ CommandCompare cc = new CommandCompare() {
|
|
|
+ @Override
|
|
|
+ public int compare(String first, String second) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ System.out.println("============");
|
|
|
+ CommandCompare cc1 = (String first, String second) -> {
|
|
|
+ System.out.println("CommandCompare lambda");
|
|
|
+ if (first.length() < second.length()) return -1;
|
|
|
+ if (first.length() > second.length()) return 1;
|
|
|
+ return 0;
|
|
|
+ };
|
|
|
+ System.out.println(cc1.compare("aaa", "bbbb"));
|
|
|
+ CommandCompare cc2 = (first, second) -> {
|
|
|
+ return Integer.compare(first.length(),second.length());
|
|
|
+ };
|
|
|
+
|
|
|
+ CommandCompare cc3 =
|
|
|
+ (first, second) -> Integer.compare(first.length(),second.length());
|
|
|
+ System.out.println(cc3.compare("ccc", "ddd"));
|
|
|
+
|
|
|
+
|
|
|
+ System.out.println("============");
|
|
|
+ CommandMethod cm = Math::max;
|
|
|
+ CommandMethod cm1 = Math::min;
|
|
|
+ CommandMethod cm2 = (int x,int y) -> { return Math.max(x,y);};
|
|
|
+ CommandMethod cm3 = ( x, y) -> Math.max(x,y);
|
|
|
+
|
|
|
+ System.out.println("============");
|
|
|
+ // 调用
|
|
|
+ System.out.println(subStr("我Code,我快乐", str -> str.substring(0, 5)));
|
|
|
+ // 匿名内部类
|
|
|
+ subStr("数据", new Function<String, String>() {
|
|
|
+ @Override
|
|
|
+ public String apply(String s) {
|
|
|
+ return "处理逻辑";
|
|
|
+ }
|
|
|
+ });
|
|
|
+ subStr("数据",(String s)->{return "处理逻辑";});
|
|
|
+ subStr("数据", s-> "处理逻辑");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 声明方法 y=f(x) R=f(T) String=f(String)
|
|
|
+ public static String subStr(String str, Function<String, String> function){
|
|
|
+ return function.apply(str);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|