Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 11 additions & 34 deletions src/main/java/com/github/hcsp/functional/RefactorToConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,24 @@ public static void main(String[] args) {
public static void printWithConsumer(
Map<String, String> map1,
Map<String, String> map2,
BiConsumer<String, String> consumer) {}
BiConsumer<String, String> consumer) {
map1.forEach(consumer);
map2.forEach(consumer);
}

public static void printWithComma(Map<String, String> map1, Map<String, String> map2) {
for (Map.Entry<String, String> entry : map1.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + "," + value);
}
static BiConsumer<Map<String, String>, Map<String, String>> printPrefix(String prefix) {
return ((map, map2) -> printWithConsumer(map, map2, (k, v) -> System.out.println(k + prefix + v)));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return 值周围不必要的小括号。
用一个空格分隔非空白字符。

}

for (Map.Entry<String, String> entry : map2.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + "," + value);
}
public static void printWithComma(Map<String, String> map1, Map<String, String> map2) {
printPrefix(",").accept(map1, map2);
}

public static void printWithDash(Map<String, String> map1, Map<String, String> map2) {
for (Map.Entry<String, String> entry : map1.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + "-" + value);
}

for (Map.Entry<String, String> entry : map2.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + "-" + value);
}
printPrefix("-").accept(map1, map2);
}

public static void printWithColon(Map<String, String> map1, Map<String, String> map2) {
for (Map.Entry<String, String> entry : map1.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + ":" + value);
}

for (Map.Entry<String, String> entry : map2.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + ":" + value);
}
printPrefix(":").accept(map1, map2);
}
}
30 changes: 9 additions & 21 deletions src/main/java/com/github/hcsp/functional/RefactorToFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,30 @@ 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方法同理
// 例如,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);
}
}
25 changes: 10 additions & 15 deletions src/main/java/com/github/hcsp/functional/RefactorToSupplier.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.github.hcsp.functional;

import java.rmi.server.ObjID;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无用导入 - java.rmi.server.ObjID 。

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;

public class RefactorToSupplier {
Expand All @@ -19,30 +21,23 @@ public static void main(String[] args) {
// 请尝试使用函数式接口Supplier对三个方法进行重构,消除冗余代码
// 并尽量尝试使用lambda表达式和方法引用来传递参数
public static List<Object> create(Supplier<Object> supplier) {
return null;
}

public static List<Object> createObjects() {
List<Object> result = new ArrayList<>();
for (int i = 0; i < 10; i++) {
result.add(new Object());
result.add(supplier.get());
}
return result;
}

public static List<Object> createObjects() {
return create(Object::new);
}

public static List<Object> createStrings() {
List<Object> result = new ArrayList<>();
for (int i = 0; i < 10; i++) {
result.add("" + i);
}
return result;
AtomicInteger i = new AtomicInteger(0);
return create(() -> "" + i.getAndIncrement());
}

public static List<Object> createRandomIntegers() {
List<Object> result = new ArrayList<>();
for (int i = 0; i < 10; i++) {
result.add(randomInt());
}
return result;
return create(RefactorToSupplier::randomInt);
}
}