|
@@ -0,0 +1,224 @@
|
|
|
+package com.sf.util.jdk8;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.BigInteger;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.UUID;
|
|
|
+import java.util.function.Consumer;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.function.Predicate;
|
|
|
+import java.util.function.Supplier;
|
|
|
+
|
|
|
+public class CommandTest {
|
|
|
+ public static void main(String[] args) {
|
|
|
+ // 要处理的数据
|
|
|
+ int[] nums = {3, -4, 6, 4};
|
|
|
+ // 处理逻辑
|
|
|
+ PrintCommand pc = new PrintCommand();
|
|
|
+ AddCommand ac = new AddCommand();
|
|
|
+ // 将两者结合起来 解耦
|
|
|
+ ProcessArray pa = new ProcessArray();
|
|
|
+// pa.process(target, pc);
|
|
|
+// pa.process(target, ac);
|
|
|
+ // pc.process(target)
|
|
|
+ pa.process(nums, new Command() {
|
|
|
+ public void process(int[] target) {
|
|
|
+ int res = target[0];
|
|
|
+ for (int i = 1; i < target.length; i++) {
|
|
|
+ res *= target[i];
|
|
|
+ }
|
|
|
+ System.out.println("元素乘积:" + res);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 参数列表 lambda体(方法体)
|
|
|
+ pa.process(nums, (int[] target) -> {
|
|
|
+ int res = target[0];
|
|
|
+ for (int i = 1; i < target.length; i++) {
|
|
|
+ res *= target[i];
|
|
|
+ }
|
|
|
+ System.out.println("元素乘积:" + res);
|
|
|
+ });
|
|
|
+
|
|
|
+ System.out.println("==========");
|
|
|
+ CommandSimple cs = new CommandSimple() {
|
|
|
+ @Override
|
|
|
+ public void process() {
|
|
|
+ System.out.println("hello world");
|
|
|
+ }
|
|
|
+ };
|
|
|
+ cs.process();
|
|
|
+
|
|
|
+ // 无参数 无返回值的情况
|
|
|
+ CommandSimple cs1 = () -> System.out.println("hello lambda");
|
|
|
+ cs1.process();
|
|
|
+
|
|
|
+ System.out.println("==========");
|
|
|
+ CommandOneParam cop = new CommandOneParam() {
|
|
|
+ @Override
|
|
|
+ public void process(int x) {
|
|
|
+ System.out.println(x);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ cop.process(10);
|
|
|
+
|
|
|
+ CommandOneParam cop1 = (int x) -> System.out.println(x);
|
|
|
+ cop1.process(100);
|
|
|
+
|
|
|
+ CommandOneParam cop2 = System.out::println;
|
|
|
+ cop2.process(1000);
|
|
|
+
|
|
|
+ System.out.println("==========");
|
|
|
+ // 有一个参数 & 一个返回结果的情况
|
|
|
+ CommandOneResult cor = new CommandOneResult() {
|
|
|
+ @Override
|
|
|
+ public int process(int x) {
|
|
|
+ return x;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ System.out.println(cor.process(11));
|
|
|
+
|
|
|
+ // 如果是返回逻辑 一行也不能省略大括号
|
|
|
+ CommandOneResult cor1 = (int x) -> {
|
|
|
+ return x;
|
|
|
+ };
|
|
|
+ System.out.println(cor1.process(101));
|
|
|
+
|
|
|
+ CommandOneResult cor2 = (int x) -> x;
|
|
|
+ System.out.println(cor2.process(202));
|
|
|
+
|
|
|
+ System.out.println("==========");
|
|
|
+ // 多个参数 & 多个返回结果的情况
|
|
|
+ CommandCompare cc = new CommandCompare() {
|
|
|
+ @Override
|
|
|
+ public int compare(String first, String second) {
|
|
|
+ if (first.length() < second.length()) return -1;
|
|
|
+ else if (first.length() > second.length()) return 1;
|
|
|
+ else return 0;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ System.out.println(cc.compare("11", "222"));
|
|
|
+
|
|
|
+ CommandCompare cc1 = (String first, String second) -> {
|
|
|
+ if (first.length() < second.length()) return -1;
|
|
|
+ else if (first.length() > second.length()) return 1;
|
|
|
+ else return 0;
|
|
|
+ };
|
|
|
+ System.out.println(cc1.compare("111", "22"));
|
|
|
+
|
|
|
+
|
|
|
+ CommandCompare cc2 = (String first, String second) -> {
|
|
|
+ return Integer.compare(first.length(), second.length());
|
|
|
+ };
|
|
|
+ System.out.println(cc2.compare("111", "222"));
|
|
|
+
|
|
|
+
|
|
|
+ CommandCompare cc3 = (first, second) -> Integer.compare(first.length(), second.length());
|
|
|
+ System.out.println(cc3.compare("222", "1111"));
|
|
|
+
|
|
|
+ System.out.println("==========");
|
|
|
+ String[] arr = {"12", "123", "1234", "1", "23", "234"};
|
|
|
+// Arrays.sort(arr);
|
|
|
+// Arrays.sort(arr,new LengthComparator());
|
|
|
+// System.out.println(Arrays.toString(arr));
|
|
|
+
|
|
|
+// Arrays.sort(arr, new Comparator<String>() {
|
|
|
+// @Override
|
|
|
+// public int compare(String o1, String o2) {
|
|
|
+// return Integer.compare(o1.length(), o2.length());
|
|
|
+// }
|
|
|
+// });
|
|
|
+// System.out.println(Arrays.toString(arr));
|
|
|
+
|
|
|
+ // 将逻辑传输到方法中 最简代码块
|
|
|
+ Arrays.sort(arr, (o1, o2) -> Integer.compare(o1.length(), o2.length()));
|
|
|
+ System.out.println(Arrays.toString(arr));
|
|
|
+
|
|
|
+ // 方法引用
|
|
|
+ // x -> System.out.println(x)
|
|
|
+ // 当入参 和 处理逻辑中的参数完全相同时 参数可以省略 所以箭头可以没有
|
|
|
+ // 使用 :: 代表 这是lambda表达式
|
|
|
+ // 谁的(::)方法
|
|
|
+ // System.out::println;
|
|
|
+
|
|
|
+// CommandOneParam cop2 = System.out::println;
|
|
|
+// cop2.process(1000);
|
|
|
+
|
|
|
+ System.out.println("==========");
|
|
|
+ CommandMethod cm = new CommandMethod() {
|
|
|
+ @Override
|
|
|
+ public int process(int x, int y) {
|
|
|
+ return Math.max(x, y);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ CommandMethod cm1 = (int x, int y) -> {
|
|
|
+ return Math.max(x, y);
|
|
|
+ };
|
|
|
+ CommandMethod cm2 = (x, y) -> Math.max(x, y);
|
|
|
+ CommandMethod cm3 = Math::max;
|
|
|
+
|
|
|
+ System.out.println(cm3.process(10, 20));
|
|
|
+ CommandMethod cm4 = Math::min;
|
|
|
+ System.out.println(cm4.process(10, 20));
|
|
|
+
|
|
|
+ System.out.println("==========");
|
|
|
+ // 方法引用第二种
|
|
|
+ // 如果两个参数 也可以将一个参数变成执行体中 方法的调用者
|
|
|
+ // 另一个参数 变成 执行体中方法的入参
|
|
|
+ CommandMethodString cms = (o1,o2) -> o1.equals(o2);
|
|
|
+ CommandMethodString cms1 = String::equals;
|
|
|
+ System.out.println(cms1.compare("123", "123"));
|
|
|
+ System.out.println(cms.compare("123", "321"));
|
|
|
+
|
|
|
+ System.out.println("==========");
|
|
|
+ // 构造方法的调用 用new来指代
|
|
|
+ // int process(int x)
|
|
|
+ CommandOneResult cor11 = Integer::valueOf;
|
|
|
+ CommandOneParam cop11 = BigDecimal::new;
|
|
|
+
|
|
|
+ // 函数式接口
|
|
|
+ System.out.println("==========");
|
|
|
+ System.out.println(subStr("我Code,我快乐", str -> str.substring(0, 5)));
|
|
|
+
|
|
|
+ print("我Code,我快乐", System.out::println);
|
|
|
+
|
|
|
+ String uuid = get(() -> UUID.randomUUID().toString());
|
|
|
+ System.out.println(uuid);
|
|
|
+
|
|
|
+ boolean result = startWith("I Like Code!", s -> s.startsWith("I"));
|
|
|
+ System.out.println(result);
|
|
|
+
|
|
|
+ // 练习
|
|
|
+ // 1、 一个参数 一个返回结果的情况
|
|
|
+ // String process(String str);
|
|
|
+ // 逻辑1 替换 "abcabcabc" -> "dbcdbcdbc"
|
|
|
+ // 逻辑2 去除空格 " abc " -> "abc"
|
|
|
+ // 逻辑3 分隔 "110,010,000,111" -> "111" (逗号个数不确定 逗号中间隔位数不确定 分隔后最后几位)
|
|
|
+ // Function<String, String> 使用
|
|
|
+
|
|
|
+ // 2、一个参数 返回true/false
|
|
|
+ // boolean process(String str);
|
|
|
+ // 逻辑1 是否包含 "abcabcabc" -> "abc"
|
|
|
+ // 逻辑2 是否结尾 "abc" -> "c"
|
|
|
+ // Predicate<String>
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String subStr(String str, Function<String, String> function){
|
|
|
+ return function.apply(str);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void print(String s, Consumer<String> consumer){
|
|
|
+ consumer.accept(s);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String get(Supplier<String> supplier){
|
|
|
+ return supplier.get();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean startWith(String str, Predicate<String> predicate){
|
|
|
+ return predicate.test(str);
|
|
|
+ }
|
|
|
+}
|