How to print out all the elements of a List in Java?

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.

Leave a Comment