|
@@ -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(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);
|
|
|
+
|
|
|
+
|
|
|
+ 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-> "处理逻辑");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String subStr(String str, Function<String, String> function){
|
|
|
+ return function.apply(str);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|