[英]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.