gpt4 book ai didi

java - 使用Stream机制的filter()和map()方法的示例

转载 作者:行者123 更新时间:2023-12-01 12:03:45 27 4
gpt4 key购买 nike

我正在阅读有关 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com