What Is Iterator Too Listiterator Inward Coffee Plan Amongst Illustration - Tutorial Code Sample
Wednesday, July 11, 2018
Add Comment
Iterator inward Java is aught but a traversing object, made specifically for Collection objects similar List and Set. nosotros convey already aware near dissimilar form of traversing methods similar for-loop ,while loop,do-while,for each lop etc,they all are index based traversing but equally nosotros know Java is purely object oriented language at that topographic point is ever possible ways of doing things using objects as well as then Iterator is a agency to traverse equally good equally access the information from the collection. Even alongside traversing alongside object nosotros convey Enumeration, Iterator as well as ListIterator inward Java which nosotros volition inward this Java Iterator tutorial.
What is Iterator inward Java API:
Java iterator is an interface belongs to collection framework allow us to traverse the collection as well as access the information chemical component subdivision of collection without bothering the user near specific implementation of that collection it. Basically List as well as laid collection provides the iterator. You tin larn Iterator from ArrayList, LinkedList, as well as TreeSet etc. Map implementation such equally HashMap doesn’t supply Iterator directory but you lot tin larn at that topographic point keySet or Value Set as well as tin iterator through that collection. Syntax:
It comes within java.util package. equally public interface Iterator and contains 3 methods:
boolean hasNext(): this method returns truthful if this Iterator has to a greater extent than chemical component subdivision to iterate.
Object next(): return the side past times side chemical component subdivision inward the collection until the hasNext() method render true. Its ever recommended to telephone band hasNext() method earlier calling next() method to avoid java.util.NoSuchElementException: Hashtable Iterator
remove(): method take the terminal chemical component subdivision render past times the iterator this method solely calls i time per telephone band to next().
How to create Iterator inward Java:
Every collection classes provides an iterator() method that returns an iterator to the kickoff of the collection. By using this iterator object, nosotros tin access each chemical component subdivision inward the collection, i chemical component subdivision at a time. In general, to purpose an iterator to traverse through the contents of a collection follow these steps:
1. First obtain an iterator to the beginning of the collection past times calling the collection's iterator( )
2. Set upward a loop that makes a telephone band to hasNext( ). Have the loop iterate equally long equally hasNext( ) returns true.
3. Within the loop, obtain each chemical component subdivision past times calling next( ).
1. First obtain an iterator to the beginning of the collection past times calling the collection's iterator( )
2. Set upward a loop that makes a telephone band to hasNext( ). Have the loop iterate equally long equally hasNext( ) returns true.
3. Within the loop, obtain each chemical component subdivision past times calling next( ).
Java Iterator tutorial as well as Example
Example of How to purpose Iterator inward Java
Here is consummate code instance of How to purpose Iterator inward Java. This Java programme creates Hashtable, populate it alongside dummy information as well as than uses Iterator to traverse Map as well as prints fundamental as well as value for each Entry. This Java Program uses Generic version of Iterator to ensure type-safety as well as avoid casting .
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* Simple Java Program which shows How to purpose Iterator inward Java to loop through all elements
* of Collection classes similar ArrayList, LinkedList, HashSet or Map implementation like
* HashMap,TreeMap as well as Hashtable.
*/
public class IteratorExample{
public static void main(String[] args) {
//Hashtable instance used for Iterator Example inward Java
Hashtable<Integer, String> stockTable=new Hashtable<Integer,String>();
//Populating Hashtable instance alongside sample values
stockTable.put(new Integer(1), "Two");
stockTable.put(new Integer(2), "One");
stockTable.put(new Integer(4), "Four");
stockTable.put(new Integer(3), "Three");
//Getting Set of keys for Iteration
Set<Entry<Integer, String>> stockSet = stockTable.entrySet();
// Using Iterator to loop Map inward Java, hither Map implementation is Hashtable
Iterator<Entry<Integer, String>> i= stockSet.iterator();
System.out.println("Iterating over Hashtable inward Java");
//Iterator begins
while(i.hasNext()){
Map.Entry<Integer,String> m=i.next();
int fundamental = m.getKey();
String value=m.getValue();
System.out.println("Key :"+key+" value :"+value);
}
System.out.println("Iteration over Hashtable finished");
}
}
Output:
Iterating over Hashtable inward Java
Key :4 value :Four
Key :3 value :Three
Key :2 value :One
Key :1 value :Two
Iteration over Hashtable finished
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* Simple Java Program which shows How to purpose Iterator inward Java to loop through all elements
* of Collection classes similar ArrayList, LinkedList, HashSet or Map implementation like
* HashMap,TreeMap as well as Hashtable.
*/
public class IteratorExample{
public static void main(String[] args) {
//Hashtable instance used for Iterator Example inward Java
Hashtable<Integer, String> stockTable=new Hashtable<Integer,String>();
//Populating Hashtable instance alongside sample values
stockTable.put(new Integer(1), "Two");
stockTable.put(new Integer(2), "One");
stockTable.put(new Integer(4), "Four");
stockTable.put(new Integer(3), "Three");
//Getting Set of keys for Iteration
Set<Entry<Integer, String>> stockSet = stockTable.entrySet();
// Using Iterator to loop Map inward Java, hither Map implementation is Hashtable
Iterator<Entry<Integer, String>> i= stockSet.iterator();
System.out.println("Iterating over Hashtable inward Java");
//Iterator begins
while(i.hasNext()){
Map.Entry<Integer,String> m=i.next();
int fundamental = m.getKey();
String value=m.getValue();
System.out.println("Key :"+key+" value :"+value);
}
System.out.println("Iteration over Hashtable finished");
}
}
Output:
Iterating over Hashtable inward Java
Key :4 value :Four
Key :3 value :Three
Key :2 value :One
Key :1 value :Two
Iteration over Hashtable finished
Why purpose Iterator when nosotros convey Enumerator:
Both Iterator and Enumerator is used for traversing of collection but Iterator is to a greater extent than enhanced inward damage of extra method remove () it supply us for alter the collection which is non available inward enumeration along alongside that it is to a greater extent than secure it doesn’t allow only about other thread to alter the collection when only about some other thread is iterating the collection as well as throws concurrentModificationException. Along alongside this method mention are likewise real convenient inward Iterator which is non major deviation but brand purpose of iterator to a greater extent than easy.
What is List Iterator inward Java?
ListIterator in Java is an Iterator which allows user to traverse Collection similar HashSet in both management past times using method previous() and next (). You tin obtain ListIterator from all List implementation including ArrayList as well as LinkedList. ListIterator doesn’t conk along electrical current index as well as its electrical current seat is determined past times telephone band to next() or previous() based on management of traversing.
Important signal near Iterator inward Java:
1) Iterator inward Java back upward generics as well as then ever purpose Generic version of Iterator rather than using Iterator alongside raw type.
2) If you lot desire to take objects from Collection than don't purpose for-each loop instead purpose Iterator's remove() method to avoid whatever ConcurrentModificationException.
3) Iterating over collection using Iterator is discipline to ConcurrentModificationException if Collection is modified later Iteration started, but this solely happens inward instance of fail-fast Iterators.
4) There are ii types of Iterators inward Java, fail-fast as well as fail-safe, banking concern represent difference betwixt fail-safe as well as fail-fast Iterator for to a greater extent than details.
5) List collection type likewise supports ListIterator which has add() method to add together elements inward collection piece Iterating. There are only about to a greater extent than differences betwixt Iterator as well as ListIterator similar bidirectional traversing, which nosotros convey discussed above. Also why ListIterator has add() method is a popular Java Collection Interview question.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Advanced usage of Enum inward Java alongside Example
0 Response to "What Is Iterator Too Listiterator Inward Coffee Plan Amongst Illustration - Tutorial Code Sample"
Post a Comment