DEV Community

Ramesh Fadatare
Ramesh Fadatare

Posted on • Originally published at sourcecodeexamples.net

Java Stream map() Example

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:


[1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Related Java Stream API's Examples

  1. Java Stream filter() Example
  2. Java Stream map() Example
  3. Java Stream flatMap() Example
  4. Java Stream distinct() Example
  5. Java Stream limit() Example
  6. Java Stream peek() Example
  7. Java Stream anyMatch() Example
  8. Java Stream allMatch() Example
  9. Java Stream noneMatch() Example
  10. Java Stream collect() Example
  11. Java Stream count() Example
  12. Java Stream findAny() Example
  13. Java Stream findFirst() Example
  14. Java Stream forEach() Example
  15. Java Stream min() Example
  16. Java Stream max() Example
  17. Java Stream reduce() Example
  18. Java Stream toArray() Example

Top comments (0)