This post will discuss how to traverse from the second element of a List in Java.

1. Using List.subList() method

You can use the subList() method to iterate over the sublist of the elements starting from the second index till the end of the list. This approach does not create an intermediary list object, as the subList() method returns a view of the list is backed by the original list.

Download  Run Code

Output:

C++
Java
Ruby
Kotlin

 
Note that the starting index is inclusive, and the ending index is exclusive in the subList() method. Here’s an equivalent version using the Stream API:

Download  Run Code

Output:

C++
Java
Ruby
Kotlin

2. Using Stream.skip() method

With Java 8, you can call the skip() method to discard the first element of the stream, and process the remaining elements. This is demonstrated below:

Download  Run Code

Output:

C++
Java
Ruby
Kotlin

3. Using ListIterator

Here’s another plausible way to traverse from the second element of a List. The idea is to create a list iterator over the elements in the list, starting at the second position in the list.

Download  Run Code

Output:

C++
Java
Ruby
Kotlin

4. Using for loop

The regular for loop can come in very handy for this trivial task. Following is a simple example demonstrating the usage of this approach:

Download  Run Code

Output:

C++
Java
Ruby
Kotlin

 
Here’s an equivalent version using IntStream.

Download  Run Code

Output:

C++
Java
Ruby
Kotlin

 
If you prefer the enhanced for loop, you can maintain a boolean flag to skip the first element.

Download  Run Code

Output:

C++
Java
Ruby
Kotlin

That’s all about traversing from the second element of a List in Java.