【问题标题】:example of using filter() & map() method of Stream mechanismStream机制的filter()和map()方法使用示例
【发布时间】:2015-01-07 12:56:53
【问题描述】:

我正在阅读有关 java 8 的 Stream 机制及其不同方法的信息,我想尝试用它来映射和排序字符串数据,但我无法理解 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 类的 filter 和 map 方法?

【问题讨论】:

标签: java java-8


【解决方案1】:

这是一个例子

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

【讨论】:

    猜你喜欢
    • 2016-05-17
    • 2018-06-27
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    • 2021-05-22
    • 2019-02-14
    相关资源
    最近更新 更多