Java Stream How to - Map to add String value to each element








Question

We would like to know how to map to add String value to each element.

Answer

//from   www.j  ava2 s.com
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Main {

  public static void main(String[] args) {
    List<String> _names = Arrays.asList("Bob", "Tom", "Jeff", "Scott",
        "Jennifer", "Steve");

    List<String> greetings = _names.stream().map(name -> "Hello " + name)
        .collect(Collectors.toList());
    System.out.println(greetings);

  }
}

The code above generates the following result.