簡體   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