How to traverse a linked list in Java?

To iterate a LinkedList in Java, use the for-each loop like so: for (var item : list).

Here's how you do it:

var linkedList = new java.util.LinkedList<String>();
linkedList.add("Hello");
linkedList.add("World");
linkedList.add("!");

// Hello
// World
// !
for (var item : linkedList) {
  System.out.println(item);
}