1. Introduction
Iterating is a cornerstone of programming, enabling developers to traverse and easily manipulate data structures. However, there are situations where we may need to iterate over these collections while skipping the first element. In this tutorial, we’ll explore various methods to skip the first element using loops and the Stream API.
2. Skipping the First Element
There are various ways to skip the first element in Java when iterating through a collection. Below, we cover three major approaches: using standard loops, using the Iterator interface, and utilizing the Stream API.
Some algorithms require skipping the first element for different reasons: to process it separately or skip it entirely, for example, CSV headers. Calculating running differences between the daily income of a small business might be a good example of this approach:
Iterating from the second element helps us to create a simple algorithm and saves us from unnecessary and non-intuitive checks.
2.1. For Loop
The simplest way to skip the first element is to use a for loop and start the counter variable from 1 instead of 0. This approach is most suitable for collections that support indexed access, like ArrayList and simple arrays:
void skippingFirstElementInListWithForLoop(List<String> stringList) {
for (int i = 1; i < stringList.size(); i++) {
process(stringList.get(i));
}
}
The main benefit of this approach is its simplicity. Also, it allows us to access the first value if we would like to do so. At the same time, setting the initial value might be easily overlooked.
2.2. While Loop
Another way is to use a while loop along with an Iterator. We can manually advance the iterator to skip the first element:
void skippingFirstElementInListWithWhileLoop(List<String> stringList) {
Iterator<String> iterator = stringList.iterator();
if (iterator.hasNext()) {
iterator.next();
}
while (iterator.hasNext()) {
process(iterator.next());
}
}
One of the benefits of this method is the fact that it works with all the Iterable collections, such as Set:
void skippingFirstElementInSetWithWhileLoop(Set<String> stringSet) {
Iterator<String> iterator = stringSet.iterator();
if (iterator.hasNext()) {
iterator.next();
}
while (iterator.hasNext()) {
process(iterator.next());
}
}
An additional benefit is that we can abstract the collection to the most general class; in this case, it would be Iterable and reuse the code. However, as Sets, in most cases, don’t have the notion of order, it’s not clear which element we’ll skip. On the other side, if we need the first element, we have to store it explicitly:
void skippingFirstElementInListWithWhileLoopStoringFirstElement(List<String> stringList) {
Iterator<String> iterator = stringList.iterator();
String firstElement = null;
if (iterator.hasNext()) {
firstElement = iterator.next();
}
while (iterator.hasNext()) {
process(iterator.next());
// additional logic using fistElement
}
}
2.3. Stream API
Java 8 introduced the Stream API, which provides a more declarative way to manipulate collections. To skip the first element, we can use the skip() method:
void skippingFirstElementInListWithStreamSkip(List<String> stringList) {
stringList.stream().skip(1).forEach(this::process);
}
Like the previous one, this method works on Iterable collection and is quite verbose about its intentions. We can easily use Set with this approach:
void skippingFirstElementInSetWithStreamSkip(Set<String> stringSet) {
stringSet.stream().skip(1).forEach(this::process);
}
Also, we can use a Map:
void skippingFirstElementInMapWithStreamSkip(Map<String, String> stringMap) {
stringMap.entrySet().stream().skip(1).forEach(this::process);
}
Maps, similar to Sets, don’t maintain the order of the elements, so please use this with caution, as the first element isn’t clearly defined in this case. However, sometimes skipping an element in a Set or in a Map might be useful.
2.4. Using subList()
Another way to skip the first element in Lists is using the subList() method. This method returns a view of the portion of the list between the specified fromIndex, inclusive, and toIndex, exclusive. When we pair this with a for-each loop, we can easily skip the first element:
void skippingFirstElementInListWithSubList(List<String> stringList) {
for (final String element : stringList.subList(1, stringList.size())) {
process(element);
}
}
One of the issues is that subList() can fail on empty lists. Another thing is that it’s not clear, especially for people new to Java, that it doesn’t create a separate collection and provides a view of the original one. Overall, this is a highly imperative way of iterating.
2.5. Other Methods
Although there are only a handful of fundamental ways to iterate over a collection, we could combine and alter them to get more variations. As these variations aren’t substantially different, we list them here to show possible approaches and inspire experimentation.
We can skip the first element using a for loop with an additional if statement. It’s useful when we need to process the first element separately from the others:
void skippingFirstElementInListWithForLoopWithAdditionalCheck(List<String> stringList) {
for (int i = 0; i < stringList.size(); i++) {
if (i == 0) {
// do something else
} else {
process(stringList.get(i));
}
}
}
We can apply the logic we’re using in a for loop to a while loop and use a counter inside it:
void skippingFirstElementInListWithWhileLoopWithCounter(List<String> stringList) {
int counter = 0;
while (counter < stringList.size()) {
if (counter != 0) {
process(stringList.get(counter));
}
++counter;
}
}
This approach might be helpful when we need to advance the list in different steps depending on some internal logic.
Also, we can combine the approach with subList and with stream:
void skippingFirstElementInListWithStreamSkipAndSubList(List<String> stringList) {
stringList.subList(1, stringList.size()).forEach(this::process);
}
Overall, our imagination can bring us to some esoteric decisions that should not be used at all:
void skippingFirstElementInListWithReduce(List<String> stringList) {
stringList.stream().reduce((skip, element) -> {
process(element);
return element;
});
}
3. Conclusion
While Java offers different ways to iterate a collection while skipping the first element, the primary metric in picking the right approach is the clarity of the code. Thus, we should opt for two simple and the most verbose methods, which are simple for loop or stream.skip(). Other methods are more complex, contain more moving parts, and should be avoided if possible.
As usual, the examples from this article are available over on GitHub.
Find A Teacher Form:
https://docs.google.com/forms/d/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ/viewform?edit_requested=true#responses
Email:
public1989two@gmail.com
www.itsec.hk
www.itsec.vip
www.itseceu.uk
Leave a Reply