我正在阅读有关 Stream
的内容java 8 的机制及其不同的方法,我想尝试用它来映射和排序字符串数据,但我无法理解 Stream
文档。
来自 map
的 Java 8 文档方法
<R> Stream<R> map(Function<? super T,? extends R> mapper)
Returns a stream consisting of the results of applying the given function to the elements of this stream.
和过滤方法,
Stream<T> filter(Predicate<? super T> predicate)
Returns a stream consisting of the elements of this stream that match the given predicate.
任何人都可以给出使用 Stream 类的过滤器和映射方法的任何实际示例吗?
最佳答案
这是一个例子
List<String> list= Arrays.asList("x1", "x2", "y1", "y2", "z1");
list
.stream()
.filter(s -> s.startsWith("x"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);
关于java - 使用Stream机制的filter()和map()方法的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27820036/