By Alvin Alexander. Last updated: April 6, 2024
As a quick Java tip related to lists, I was just reminded that if you need to print every element in a Java List, you can use the forEach
method on the List
:
// [1] create a List of strings. java.util.List<String> listOfStrings = CollectionConverters.asJava(xs); // [2] print the List of strings using forEach and System.out.println. // note that there is no need for a 'for' loop. listOfStrings.forEach(System.out::println);
I can confirm that as of August, 2021, this solution works just fine. So if you ever need to print every element in a Java List
— without using a for
loop — I hope this example is helpful.
this post is sponsored by my books: | |||
![]() #1 New Release |
![]() FP Best Seller |
![]() Learn Scala 3 |
![]() Learn FP Fast |