Java 8 - Stream map() method example

Posted on January 12, 2017


The Stream#map() method consumes a single element from input stream and produces a single element to output stream. For example –

Stream[1, 2, 3, 4, 5, 6]  ⇒ map(n → n*n) ⇒ Stream[1 ,4, 9, 16, 25, 36]

You may also like - Java 8 - Stream flatMap() method example

The following examples demonstrate how to use the map() method of the java.util.stream.Stream interface with collection types List and Map.

Map + Stream map() example

The following example shows how to retrieve values from the Map using the map() method of the Stream.

StreamMapExample1.java

package com.boraji.tutorial;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author imssbora
 */
public class StreamMapExample1 {
  public static void main(String[] args) {
    Map<String, String> colors = new HashMap<>();
    colors.put("#000000", "BLACK");
    colors.put("#FF0000", "RED");
    colors.put("#800000", "MAROON");
    colors.put("#FFFF00", "YELLOW");
    colors.put("#C0C0C0", "SILVER");
    colors.put("#808000", "OLIVE");
    colors.put("#808000", "OLIVE");

    // Normal method to get keys
    Set<String> colorHexCodes = getColorHexCodes(colors);
    System.out.println(colorHexCodes);

    // Using Stream map() method to get color names
    Set<String> colorNames = getColorNames(colors);
    System.out.println(colorNames);
  }

  private static Set<String> getColorHexCodes(Map<String, String> colors) {
    return colors.keySet();
  }

  private static Set<String> getColorNames(Map<String, String> colors) {
    Set<String> colorNames = colors.entrySet()
        .stream().map(e -> e.getValue())
        .collect(Collectors.toSet());
    return colorNames;
  }
}

Output

[#C0C0C0, #FFFF00, #808000, #FF0000, #000000, #800000]
[RED, SILVER, OLIVE, BLACK, YELLOW, MAROON]

 

List + Stream map() example

The following example converts the elements of list to uppercase using the map() method of the Stream.

StreamMapExample2.java

package com.boraji.tutorial;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author imssbora
 */
public class StreamMapExample2 {
  public static void main(String[] args) {
    List<String> colors = new ArrayList<>();
    colors.add("Red");
    colors.add("Pink");
    colors.add("Orange");
    colors.add("Yellow");
    colors.add("Violet");
    colors.add("Indigo");
    colors.add("Green");

    // Using normal method to convert color names to upper-case
    List<String> list1 = getColorInUpperCase1(colors);
    System.out.println(list1);

    // Using Stream map() method to convert color names to upper-case
    List<String> list2 = getColorInUpperCase2(colors);
    System.out.println(list2);
  }

  private static List<String> getColorInUpperCase1(List<String> colors) {
    List<String> list = new ArrayList<>();
    for (String color : colors) {
      list.add(color.toUpperCase());
    }
    return list;
  }

  private static List<String> getColorInUpperCase2(List<String> colors) {
    List<String> list = colors.stream()
        .map(e -> e.toUpperCase())
        .collect(Collectors.toList());
    return list;
  }

}

Output

[RED, PINK, ORANGE, YELLOW, VIOLET, INDIGO, GREEN]
[RED, PINK, ORANGE, YELLOW, VIOLET, INDIGO, GREEN]