What is the use-case for Iterator and for loop (not for-each loop)
Please explain with example.
What is the use-case for Iterator and for loop (not for-each loop)
Please explain with example.
Besides the fact that an iterator can check if there is a next element (hasNext()
) before it fetches it - it can also remove an element from the collection. If you run in a loop and try to remove an element - what should be the "next" element ?
The Iterator interface (and its sub-interfaces) implementations provide an easy, safe, fail-fast unsynchronized mechanism to modify as well as iterate a collection. It saves the boilerplate of implementing it by yourself and provides an efficient and bug-free tool for the developer.
Foreach loop is implemented by iterator, so by using it you're not doing anything differently - it's a good tool when all you need is a simple "read only" iteration.
Don't you like simplicity ? What would you say when you are introduced to the topic of closures ? Would you shun it like you shun collection-based for loops ?
The more 'compact' the code is, the easier it becomes to understand and thus easier to maintain.
for(String each : anArrayList){
// do something
}
for(int i = 0; i < anArrayList.size(); i++){
// do something
}
The first one uses an iterator while the second uses the more 'traditional' for loop. The intent of what the programmer wants to do becomes clearer in the first one. Plus, it is easier to understand.
Also, you don't just traverse, you can do a whole lot when iterating. Like remove an element from a collection, add it to another, perform computations on it and so on...
SSCCE:
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
public class AnIterator {
public static void main(String[] args) {
List<String> list1 = new ArrayList<>();
List<String> list2 = new ArrayList<>();
list1.add("One");
list1.add("Two");
list1.add("Three");
list1.add("Four");
list1.add("Five");
Iterator it = list1.iterator();
while(it.hasNext()){
String next = (String) it.next();
String temp = next.toLowerCase();
char x = temp.charAt(0);
if(x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'){
it.remove();
list2.add(next);
}
}
System.out.println("Vowels: " + list2);
System.out.println("Consonants: " + list1);
}
}
The SSCCE shows how you can remove an element from a collection using an Iterator
. is it easier to understand than traditional for loop ? =)
Contrast it with this:
for(int i = 0; i < list1.size(); i++){
String element = list1.get(i);
String temp = element.toLowerCase();
// same logic goes here
}
Iterator is more elegant ?
If a class is iterable, then we know that we can get an iterator and use said iterator to navigate through the data structure. We don't need to know what the class looks like or how it works, etc. All we need to know are the methods for iterator.
Iterator can be used for example if you would like to remove items while traversing a collection. For
loop is just a convenience to reduce code size and code readability if do not require any specific functions of iterator.
for(Iterator<Integer> iter = list.iterator(); iter.hasNext(); )
{
int nextInt = iter.next();
if(nextInt==0)
{
iter.remove();
}
}
As far as I know the foreach loop is using internal the iterator aswell.
This could also be interesting for you: Performance of traditional for loop vs Iterator/foreach in Java