简体   繁体   中英

example of using filter() & map() method of Stream mechanism

I am reading about Stream mechanism of java 8 and its different methods and I would like to experiment with it to map and sort string data but i couldn't be able to understand the Stream documentation.

from Java 8 doc of map method

<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.

and the filter method,

Stream<T> filter(Predicate<? super T> predicate)
Returns a stream consisting of the elements of this stream that match the given predicate.

can anybody gives any real-life example to use filter and map method of Stream class ?

Here is an example

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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