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);
Answer from Holger on Stack OverflowYou 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());
Videos
Instead of printing each item in the list, I want to know if there is a way to print the List it self, like so.
int[] arr = {1, 2};
int[] arr2 = {11, 22};
List<int[]> myList = new ArrayList<>();
myList.add(arr);
myList.add(arr2);Pseudo code:
System.out.print(myList)
// {{1,2},{11,22}}Thank you.
The following is compact and avoids the loop in your example code (and gives you nice commas):
System.out.println(Arrays.toString(list.toArray()));
However, as others have pointed out, if you don't have sensible toString() methods implemented for the objects inside the list, you will get the object pointers (hash codes, in fact) you're observing. This is true whether they're in a list or not.
Since Java 8, List inherits a default "forEach" method which you can combine with the method reference "System.out::println" like this:
list.forEach(System.out::println);