通过Twitter,我发现了博客文章.由于我目前正在探索Java 8,我尝试在其中运行示例.有一件事是我无法在示例中找到类Lists
(实现a 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!
Run Code Online (Sandbox Code Playgroud)
任何人都知道我可以在哪里找到它以使示例运行?
谢谢
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());
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
134 次 |
最近记录: |