Traverse from the second element of a List in Java
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> languages = Arrays.asList("C", "C++", "Java", "Ruby", "Kotlin"); for (String language : languages.subList(1, languages.size())) { System.out.println(language); } } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> languages = Arrays.asList("C", "C++", "Java", "Ruby", "Kotlin"); languages.subList(1, languages.size()).forEach(System.out::println); } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> languages = Arrays.asList("C", "C++", "Java", "Ruby", "Kotlin"); languages.stream().skip(1).forEach(System.out::println); } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.List; import java.util.ListIterator; class Main { public static void main(String[] args) { List<String> languages = Arrays.asList("C", "C++", "Java", "Ruby", "Kotlin"); ListIterator<String> it = languages.listIterator(1); while (it.hasNext()) { String item = it.next(); System.out.println(item); } } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> languages = Arrays.asList("C", "C++", "Java", "Ruby", "Kotlin"); for (int i = 1; i < languages.size(); i++) { String item = languages.get(i); System.out.println(item); } } } |
Output:
C++
Java
Ruby
Kotlin
Here’s an equivalent version using IntStream.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; import java.util.List; import java.util.stream.IntStream; class Main { public static void main(String[] args) { List<String> languages = Arrays.asList("C", "C++", "Java", "Ruby", "Kotlin"); IntStream.range(1, languages.size()) .mapToObj(languages::get) .forEach(System.out::println); } } |
Output:
C++
Java
Ruby
Kotlin
If you prefer the enhanced for loop, you can maintain a boolean flag to skip the first element.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> languages = Arrays.asList("C", "C++", "Java", "Ruby", "Kotlin"); boolean first = true; for (String language : languages) { if (first) { first = false; continue; } System.out.println(language); } } } |
Output:
C++
Java
Ruby
Kotlin
That’s all about traversing from the second element of a List in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)