Previously we have discussed about
Here we learn about Iterator Interface in Java, but before that we must know What is Collection Interface in Java ?
Interface Iterator<E>
Type Parameters:
E - the type of elements returned by this iterator
All Known Subinterfaces:
ListIterator<E>, XMLEventReader
All Known Implementing Classes:
BeanContextSupport.BCSIterator, EventReaderDelegate, Scanner
Declaration of Iterator:
public interface Iterator<E>
Each of the collection classes provides an iterator( ) method that returns an iterator to the start of the collection.By using this iterator object, you can access each element of the collection but one element at a time.
Iterator takes the place of Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways:
1) Iterators allows to remove elements from collection during the iteration.
2) Iterators have well improved method names.
To use an iterator to access the elements of a collection, follow these steps:
1) First call the collection Iterator() method.
2) Set up a loop that makes a call to hasNext() method, as long as this method returns true.
3) Now, with in the loop get each element by calling the next() method.
This interface is a member of the Java Collections Framework.
Method Summary of Iterator:
Modifier and Method Name
|
Description
|
boolean hasNext()
|
Returns true if the iteration has more elements.
|
E next()
|
Returns the next element in the iteration.
|
void remove()
|
Removes from the underlying collection the last element returned by
this iterator (optional operation).
|
Example of Iterator:
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorInterfaceExample {
public static void main(String[] args) {
ArrayList<String> al = new ArrayList<String>();
// Add elements to the array list.
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
// Use iterator to display contents of ArrayList
System.out.println("Elements of ArrayList : ");
Iterator<String> itr = al.iterator();
while(itr.hasNext()) {
String element = itr.next();
System.out.println(element);
}
//Remove method removes all the elements from Iterator
itr.remove();
System.out.println("No Elements Found After Iterator Remove method : ");
while(itr.hasNext()) {
String element = itr.next();
System.out.println(element);
}
}
}
Program Output:
Elements of ArrayList :
C
A
E
B
D
F
No Elements Found After Iterator Remove method :
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.


No comments:
Post a Comment