我正面临着一个奇怪的问题
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collection;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Created by mklueh on 13/09/2022
*/
public class Reproducer {
private static final String COMMA_SEPARATED_STRING = "2020-05-09,hello ,10 ,11.345 ,true ,1000000000000";
/**
* Not working
*/
@Nested
public class WithReturn {
public String withReturn(Stream<String> stream, String delimiter, Function<? super String, String[]> tokenizer) {
return stream.map(s -> {
return Arrays.stream(tokenizer != null ? tokenizer.apply(s) : s.split(delimiter))
.map(String::strip) //Comment out to make it compile
.collect(Collectors.toList());
})
.flatMap(Collection::stream)
.collect(Collectors.joining(","));
}
@Test
void usingDelimiter() {
assertEquals(COMMA_SEPARATED_STRING.replaceAll(" ", ""),
withReturn(Stream.of(COMMA_SEPARATED_STRING), ",", null));
}
@Test
void usingTokenizer() {
assertEquals(COMMA_SEPARATED_STRING.replaceAll(" ", ""),
withReturn(Stream.of(COMMA_SEPARATED_STRING), null, s -> s.split(",")));
}
}
/**
* Working
*/
@Nested
public class WithReturnAndStripLambda {
public String withReturnAndStripLambda(Stream<String> stream, String delimiter, Function<? super String, String[]> tokenizer) {
return stream.map(s -> {
return Arrays.stream(tokenizer != null ? tokenizer.apply(s) : s.split(delimiter))
.map(s1 -> s1.strip())
.collect(Collectors.toList());
})
.flatMap(Collection::stream)
.collect(Collectors.joining(","));
}
@Test
void usingDelimiter() {
assertEquals(COMMA_SEPARATED_STRING.replaceAll(" ", ""),
withReturnAndStripLambda(Stream.of(COMMA_SEPARATED_STRING), ",", null));
}
@Test
void usingTokenizer() {
assertEquals(COMMA_SEPARATED_STRING.replaceAll(" ", ""),
withReturnAndStripLambda(Stream.of(COMMA_SEPARATED_STRING), null, s -> s.split(",")));
}
}
/**
* Working
*/
@Nested
public class WithMethodExpression {
public String withMethodExpression(Stream<String> stream, String delimiter, Function<? super String, String[]> tokenizer) {
return stream.map(s -> Arrays.stream(tokenizer != null ? tokenizer.apply(s) : s.split(delimiter))
.map(String::strip)
.collect(Collectors.toList())
)
.flatMap(Collection::stream)
.collect(Collectors.joining(","));
}
@Test
void usingDelimiter() {
assertEquals(COMMA_SEPARATED_STRING.replaceAll(" ", ""),
withMethodExpression(Stream.of(COMMA_SEPARATED_STRING), ",", null));
}
@Test
void usingTokenizer() {
assertEquals(COMMA_SEPARATED_STRING.replaceAll(" ", ""),
withMethodExpression(Stream.of(COMMA_SEPARATED_STRING), null, s -> s.split(",")));
}
}
}
无效的方法引用
error: incompatible types: 无效的方法引用
.map(String::strip)
^
method strip in class String cannot be applied to given types
required: no arguments
found: long
reason: actual and formal argument lists differ in length
IntelliJ的 idea 甚至建议执行自动重构,以go 掉lambda函数而支持方法引用
以及用表达式lambda替换返回
这里出了什么问题?为什么IntelliJ建议破解代码/不承认该建议导致了编译器错误?