From c53d119d007e4b33424a51d9c5932d4967f1ee90 Mon Sep 17 00:00:00 2001 From: soohea Date: Tue, 11 Jan 2022 21:42:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=BC=8F=E7=BC=96=E7=A8=8B?= =?UTF-8?q?=E7=BB=83=E4=B9=A0=EF=BC=9A=E4=BD=BF=E7=94=A8=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E5=BC=8F=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hcsp/functional/RefactorToConsumer.java | 44 +++++-------------- .../hcsp/functional/RefactorToFunction.java | 32 +++++--------- .../hcsp/functional/RefactorToSupplier.java | 23 ++++------ 3 files changed, 31 insertions(+), 68 deletions(-) diff --git a/src/main/java/com/github/hcsp/functional/RefactorToConsumer.java b/src/main/java/com/github/hcsp/functional/RefactorToConsumer.java index 25075b4..466cf98 100644 --- a/src/main/java/com/github/hcsp/functional/RefactorToConsumer.java +++ b/src/main/java/com/github/hcsp/functional/RefactorToConsumer.java @@ -20,50 +20,30 @@ public static void main(String[] args) { // 请尝试使用BiConsumer函数式接口重构下列三个方法,消除重复代码,提高可读性 // 提示:你可以使用Map.forEach方法 // 加分项:如果你能编写一个返回BiConsumer的高阶函数(即"返回函数的函数"),那就更好了 + + public static void printWithConsumer( Map map1, Map map2, - BiConsumer consumer) {} + BiConsumer consumer) { + map1.forEach(consumer); + map2.forEach(consumer); + } public static void printWithComma(Map map1, Map map2) { - for (Map.Entry entry : map1.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - System.out.println(key + "," + value); - } - - for (Map.Entry entry : map2.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - System.out.println(key + "," + value); - } + printWithConsumer(map1,map2,(k,v)->System.out.println(k + "," + v)); } + public static void printWithDash(Map map1, Map map2) { - for (Map.Entry entry : map1.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - System.out.println(key + "-" + value); - } + printWithConsumer(map1,map2,(k,v)->System.out.println(k + "-" + v)); + + - for (Map.Entry entry : map2.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - System.out.println(key + "-" + value); - } } public static void printWithColon(Map map1, Map map2) { - for (Map.Entry entry : map1.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - System.out.println(key + ":" + value); - } + printWithConsumer(map1,map2,(k,v)->System.out.println(k + ":" + v)); - for (Map.Entry entry : map2.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - System.out.println(key + ":" + value); - } } } diff --git a/src/main/java/com/github/hcsp/functional/RefactorToFunction.java b/src/main/java/com/github/hcsp/functional/RefactorToFunction.java index 94f830e..567cea5 100644 --- a/src/main/java/com/github/hcsp/functional/RefactorToFunction.java +++ b/src/main/java/com/github/hcsp/functional/RefactorToFunction.java @@ -16,7 +16,11 @@ public static void main(String[] args) { // 请尝试将下列四个方法使用IntBinaryOperator进行重构,减少重复代码 public static int[] calculate(int[] a, int[] b, IntBinaryOperator operator) { - return null; + int[] result = new int[a.length]; + for (int i = 0; i < a.length; ++i) { + result[i] = operator.applyAsInt(a[i], b[i]); + } + return result; } // 将两个数组中的每个数字分别相加,然后返回相加后的数组。你可以假定传入的数组都是等长的 @@ -24,34 +28,20 @@ public static int[] calculate(int[] a, int[] b, IntBinaryOperator operator) { // 例如,a=[1, 2, 3], b=[4, 5, 6] // 返回 [5 (1+4), 7 (2+5), 9 (3+6)] public static int[] add(int[] a, int[] b) { - int[] result = new int[a.length]; - for (int i = 0; i < a.length; ++i) { - result[i] = a[i] + b[i]; - } - return result; + return calculate(a, b, Integer::sum); } public static int[] minus(int[] a, int[] b) { - int[] result = new int[a.length]; - for (int i = 0; i < a.length; ++i) { - result[i] = a[i] - b[i]; - } - return result; + return calculate(a, b, (o1, o2) -> (o1 - o2)); } public static int[] multiply(int[] a, int[] b) { - int[] result = new int[a.length]; - for (int i = 0; i < a.length; ++i) { - result[i] = a[i] * b[i]; - } - return result; + return calculate(a, b, (o1, o2) -> (o1 * o2)); + } public static int[] divide(int[] a, int[] b) { - int[] result = new int[a.length]; - for (int i = 0; i < a.length; ++i) { - result[i] = a[i] / b[i]; - } - return result; + return calculate(a, b, (o1, o2) -> (o1 / o2)); + } } diff --git a/src/main/java/com/github/hcsp/functional/RefactorToSupplier.java b/src/main/java/com/github/hcsp/functional/RefactorToSupplier.java index eccf69e..2d6ba4f 100644 --- a/src/main/java/com/github/hcsp/functional/RefactorToSupplier.java +++ b/src/main/java/com/github/hcsp/functional/RefactorToSupplier.java @@ -19,30 +19,23 @@ public static void main(String[] args) { // 请尝试使用函数式接口Supplier对三个方法进行重构,消除冗余代码 // 并尽量尝试使用lambda表达式和方法引用来传递参数 public static List create(Supplier supplier) { - return null; - } - - public static List createObjects() { List result = new ArrayList<>(); for (int i = 0; i < 10; i++) { - result.add(new Object()); + result.add(supplier.get()); } return result; } + public static List createObjects() { + return create(Object::new); + } + public static List createStrings() { - List result = new ArrayList<>(); - for (int i = 0; i < 10; i++) { - result.add("" + i); - } - return result; + return create(String::new); } public static List createRandomIntegers() { - List result = new ArrayList<>(); - for (int i = 0; i < 10; i++) { - result.add(randomInt()); - } - return result; + return create(RefactorToSupplier::randomInt); } + }