The Java Stream map() method is an intermediate operation.
Java Stream map() Example
The Java Stream map() method converts (maps) an element to another object. For instance, if you had a list of strings it could convert each string to lowercase, uppercase, or to a substring of the original string, or something completely else.
Here is a Java Stream map() example: If you have a List of String and you want to convert that to a List of Integer, you can use map() to do so.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main
{
public static void main(String[] args)
{
List<String> listOfStrings = Arrays.asList("1", "2", "3", "4", "5");
List<Integer> listOfIntegers = listOfStrings.stream()
.map(Integer::valueOf)
.collect(Collectors.toList());
System.out.println(listOfIntegers);
}
}
Output:
[1, 2, 3, 4, 5]
Related Java Stream API's Examples
- Java Stream filter() Example
- Java Stream map() Example
- Java Stream flatMap() Example
- Java Stream distinct() Example
- Java Stream limit() Example
- Java Stream peek() Example
- Java Stream anyMatch() Example
- Java Stream allMatch() Example
- Java Stream noneMatch() Example
- Java Stream collect() Example
- Java Stream count() Example
- Java Stream findAny() Example
- Java Stream findFirst() Example
- Java Stream forEach() Example
- Java Stream min() Example
- Java Stream max() Example
- Java Stream reduce() Example
- Java Stream toArray() Example
Top comments (0)