Print list items with Java streams

You have to decide. When you want to modify the list, you can’t combine the operations. You need two statements then.

myList.replaceAll(String::toUpperCase);// modifies the list
myList.forEach(System.out::println);

If you just want to map values before printing without modifying the list, you’ll have to use a Stream:

myList.stream().map(String::toUpperCase).forEachOrdered(System.out::println);

If you want to print and save modified values simultaneously you can do

List<String> newValues = myList.stream().map(String::toUpperCase)
.peek(System.out::println).collect(Collectors.toList());

Tags:

Java

Java 8