繁体   English   中英

流机制的filter()和map()方法的使用示例

[英]example of using filter() & map() method of Stream mechanism

我正在阅读有关Java 8的Stream机制及其不同方法的信息,我想尝试使用它来映射和排序字符串数据,但我无法理解Stream文档。

从Java 8文档的map方法

<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类的filter和map方法?

这是一个例子

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM