通过推特,我发现了that blog post .
由于我目前正在探索 Java 8,因此我尝试运行其中的示例。
一件事是我找不到示例中的 Lists
类(它实现了 map
):
Function<String, String> identity = (s -> s); // return the argument
List<String> ls = Arrays.asList("Alice", "Bob", "Christine");
List<String> l2 = Lists.map(ls, identity); // [Alice, Bob, Christine] <- the same thing!
有人知道我在哪里可以找到它以使示例运行吗?
谢谢
最佳答案
JDK 和流行的库中都没有这样的方法。但是你可以自己写:
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Lists {
public static <T, R> List<R> map(List<T> input,
Function<? super T, ? extends R> mapper) {
return input.stream().map(mapper).collect(Collectors.toList());
}
}
关于java-8 - 列出 Java 8 中的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32328512/