Stream.collect()

Streams provide the collect() method, among the other methods, for stream processing on the Stream interface. When the collect() method is invoked, filtering and mapping will take place, and the object resulting from those actions will be collected. Let's take the previous example and obtain a new list of languages in uppercase, as shown in the following code:

List<String> upperCaseLanguages = languages.stream()
.map(item -> item.toUpperCase())
.collect(Collectors.toList());

System.out.println(upperCaseLanguages);

This example creates a stream, adds a map to convert the strings to uppercase, and collects all objects in a new list. We can also use the filter or sort method and collect the resulting list based on conditions applied in the filter method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.110.119