基本上我想要做的是用java 8特性编写一个像下面这样的for循环,
private String createHashCode() {
for (int i = 0; i < MAX_TRY; i++) {
final String hashCode = createRandomString();
if (!ishashCodeExistence(hashCode)) {
return hashCode;
}
}
throw new HashCodeCollisonException();
}
Run Code Online (Sandbox Code Playgroud)
我试过的是;
private String createHashCode() {
IntStream.range(0, MAX_TRY).forEach($ -> {
final String hashCode = createRandomString();
if (!ishashCodeExistence(hashCode)) {
return hashCode;
}
});
throw new HashCodeCollisonException();
}
Run Code Online (Sandbox Code Playgroud)
但是,lambda 中的 foreach 方法返回 void,因此我无法返回字符串。
有什么方法可以编写我的方法而不是使用普通的 for 循环?