Java List stream is a feature that enables the processing of elements in a List in a functional manner. It provides a way to apply operations such as filtering, mapping, or reducing on the elements of a List using functional interfaces like Predicate, Function, and Consumer.
Example 1: Filtering elements from a List of numbers
List numbers = Arrays.asList(10,21,30,45,50,60); List evenNumbers = numbers.stream() .filter(n -> n % 2 == 0) .collect(Collectors.toList());
In this example, we are creating a List of numbers and then using the List stream to filter out even numbers using the filter() method. We then collect the result of this operation in a new List.
Example 2: Mapping elements from a List of strings
List words = Arrays.asList("dog", "cat", "bird", "fish"); List wordLengths = words.stream() .map(String::length) .collect(Collectors.toList());
In this example, we are creating a List of strings and then using the List stream to map the length of each string using the map() method. We then collect the result of this operation in a new List.
Both examples use the java.util.stream package library.
Java List.stream - 30 examples found. These are the top rated real world Java examples of List.stream from package OpenDSA-server extracted from open source projects. You can rate examples to help us improve the quality of examples.