From a80a7934cbebecba2414134c7f7c27f92e069524 Mon Sep 17 00:00:00 2001 From: LatteCosmos Date: Mon, 16 Mar 2026 20:54:35 +1100 Subject: [PATCH 1/2] Using functional interface to refactor these method --- .../hcsp/functional/RefactorToConsumer.java | 43 ++++--------------- .../hcsp/functional/RefactorToFunction.java | 33 +++++--------- .../hcsp/functional/RefactorToSupplier.java | 22 +++------- 3 files changed, 26 insertions(+), 72 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..d0515cf 100644 --- a/src/main/java/com/github/hcsp/functional/RefactorToConsumer.java +++ b/src/main/java/com/github/hcsp/functional/RefactorToConsumer.java @@ -5,6 +5,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; + public class RefactorToConsumer { public static void main(String[] args) { Map map1 = @@ -23,47 +24,21 @@ public static void main(String[] args) { 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,(key,value)-> System.out.println(key+","+value)); } 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); - } - - for (Map.Entry entry : map2.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - System.out.println(key + "-" + value); - } + printWithConsumer(map1,map2,(key,value)-> 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); - } - 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) { + printWithConsumer(map1,map2,(key,value)-> 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..317340f 100644 --- a/src/main/java/com/github/hcsp/functional/RefactorToFunction.java +++ b/src/main/java/com/github/hcsp/functional/RefactorToFunction.java @@ -16,42 +16,29 @@ 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;} // 将两个数组中的每个数字分别相加,然后返回相加后的数组。你可以假定传入的数组都是等长的 - // 下列minus/multiply/divide方法同理 + // 下列 minus/multiply/divide 方法同理 // 例如,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,(x,y)->x-y); } 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,(x,y)->x*y); } 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, (x, y) -> x / y); } } diff --git a/src/main/java/com/github/hcsp/functional/RefactorToSupplier.java b/src/main/java/com/github/hcsp/functional/RefactorToSupplier.java index eccf69e..0f4ab70 100644 --- a/src/main/java/com/github/hcsp/functional/RefactorToSupplier.java +++ b/src/main/java/com/github/hcsp/functional/RefactorToSupplier.java @@ -19,30 +19,22 @@ 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); } } From 7937e52f49e1840efaaa45c7730fcabd8c0e3153 Mon Sep 17 00:00:00 2001 From: LatteCosmos Date: Tue, 17 Mar 2026 08:52:18 +1100 Subject: [PATCH 2/2] fix the white space issue --- .../java/com/github/hcsp/functional/RefactorToConsumer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/github/hcsp/functional/RefactorToConsumer.java b/src/main/java/com/github/hcsp/functional/RefactorToConsumer.java index d0515cf..61f37c7 100644 --- a/src/main/java/com/github/hcsp/functional/RefactorToConsumer.java +++ b/src/main/java/com/github/hcsp/functional/RefactorToConsumer.java @@ -30,7 +30,7 @@ public static void printWithConsumer( } public static void printWithComma(Map map1, Map map2) { - printWithConsumer(map1,map2,(key,value)-> System.out.println(key+","+value)); + printWithConsumer(map1,map2,(key,value)-> System.out.println(key+", "+value)); } public static void printWithDash(Map map1, Map map2) {