1、Java Iterator To generate successive elements from a series, we can use java iterator. It is an improvement over Enumeration interface. Iterator takes the place of Enumeration since jdk 1.2 It is a nice utility for collections. Every collection is unique on its own and imagine if we have have to w
2、rite logic on our own for every collection when there is a need to iterate it. Instead, java forces a collection to deliver an iterator. These nice utilities makes java lovable, isn’t it? Important points to note: · We can iterate only in one direction · Iteration can be done only once. If you
3、reach the end of series its done. If we need to iterate again we should get a new Iterator. Iterator Example without Generics package com.javapapers; import java.util.ArrayList; import java.util.Iterator; public class ExampleIterator { public static void main(String args[]){ ArrayLis
4、t animal = new ArrayList(); animal.add("Horse"); animal.add("Lion"); animal.add("Tiger"); Iterator animalItr = animal.iterator(); while(animalItr.hasNext()) { String animalObj = (String)animalItr.next(); System.out.println(animalObj); } } } Without generics,
5、 Iterator returns the Object and we need to typecast it.
Ads by Google
Iterator Example using Generics
package com.javapapers;
import java.util.ArrayList;
public class ExampleIterator {
public static void main(String args[]){
ArrayList
6、al.add("Horse"); animal.add("Lion"); animal.add("Tiger"); for(String animalObj : animal) { System.out.println(animalObj); } } } Output: Horse Lion Tiger Look how simple it is. There is no reference to the Iterator explicitly since we are using for-each of generics.
7、Iterable and Iterator To make an object iterable it needs to emit an Iterator object. To enforce this contract, Iterator interface is to be used. It contains a method named iterator() and it returns Iterator. Hence, any class that implements Iterable will return an Iterator. public interface Colle
8、ction
9、ct gets support for for:each loop syntax. Removing elements using Iterator · Iterator has a remove method using which we can delete elements from the underlying object. · It removes the last element returned by the iterator. Difference between Iterator and Enumeration interfaces 1. remove()
10、method is introduced in iterator. Using this method we can remove element from the underlying collection which we are iterating. 2. Enumeration has two methods and both are available in iterator. Method names for both of them are shortened. ListIterator an even better Iterator for a List contain
11、ing more utility methods like getting index of elements and adding elements to the base object. Using ListIterator we can iterate in both the directions. ConcurrentModificationException Look at the following code, it throws ConcurrentModificationException. We cannot add or remove elements to the u
12、nderlying collection when we are using an iterator. Implementing our own Custom Iterator We will create our own custom class and make it implement Iterable, so that it returns an Iterator using which we can iterate the elements. package com.javapapers; import java.util.ArrayList; public cla
13、ss ExampleIterator {
public static void main(String args[]){
ArrayList
14、 } } Output: Horse Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) at java.util.ArrayList$Itr.next(Unknown Source) at com.javapapers.ExampleIterator.main(ExampleIterator.java:13) package com.javapa
15、pers;
import java.util.ArrayList;
import java.util.Iterator;
public class AnimalIterator
16、blic boolean hasNext() { if (position < animal.size()) return true; else return false; } @Override public Object next() { Object aniObj = animal.get(position); position++; return aniObj; } @Override public void remove() { animal.remove(position); }
17、
}
package com.javapapers;
import java.util.ArrayList;
import java.util.Iterator;
public class Animal implements Iterable
18、al() {
return animal;
}
@Override
public Iterator






