What Is Iterator Together With Listiterator Inwards Coffee Programme Amongst Example - Tutorial Code Sample
Iterator inward Java is nada but a traversing object, made specifically for Collection objects similar List and Set. nosotros accept already aware close unlike variety of traversing methods similar for-loop ,while loop,do-while,for each lop etc,they all are index based traversing but every bit nosotros know Java is purely object oriented language at that spot is ever possible ways of doing things using objects together with then Iterator is a agency to traverse every bit good every bit access the information from the collection. Even amongst traversing amongst object nosotros accept Enumeration, Iterator together with 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 together with access the information chemical component division of collection without bothering the user close specific implementation of that collection it. Basically List together with laid collection provides the iterator. You tin larn Iterator from ArrayList, LinkedList, together with TreeSet etc. Map implementation such every bit HashMap doesn’t render Iterator directory but you lot tin larn at that spot keySet or Value Set together with tin iterator through that collection. Syntax:
It comes within java.util package. every bit public interface Iterator and contains 3 methods:
boolean hasNext(): this method returns truthful if this Iterator has to a greater extent than chemical component division to iterate.
Object next(): return the side past times side chemical component division 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 away the terminal chemical component division render past times the iterator this method alone calls in 1 lawsuit per telephone band to next().
How to create Iterator inward Java:
Every collection classes provides an iterator() method that returns an iterator to the showtime of the collection. By using this iterator object, nosotros tin access each chemical component division inward the collection, 1 chemical component division 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 upwardly a loop that makes a telephone band to hasNext( ). Have the loop iterate every bit long every bit hasNext( ) returns true.
3. Within the loop, obtain each chemical component division past times calling next( ).
1. First obtain an iterator to the beginning of the collection past times calling the collection's iterator( )
2. Set upwardly a loop that makes a telephone band to hasNext( ). Have the loop iterate every bit long every bit hasNext( ) returns true.
3. Within the loop, obtain each chemical component division past times calling next( ).
Java Iterator tutorial together with Example
Example of How to purpose Iterator inward Java
Here is consummate code illustration of How to purpose Iterator inward Java. This Java programme creates Hashtable, populate it amongst dummy information together with than uses Iterator to traverse Map together with prints commutation together with value for each Entry. This Java Program uses Generic version of Iterator to ensure type-safety together with 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 together with 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 amongst 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 commutation = 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 together with 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 amongst 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 commutation = 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 accept 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 render us for modify the collection which is non available inward enumeration along amongst that it is to a greater extent than secure it doesn’t allow unopen to other thread to modify the collection when unopen to some other thread is iterating the collection together with throws concurrentModificationException. Along amongst this method cite are likewise really 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 together with LinkedList. ListIterator doesn’t proceed electrical flow index together with its electrical flow seat is determined past times telephone band to next() or previous() based on management of traversing.
Important indicate close Iterator inward Java:
1) Iterator inward Java back upwardly generics together with then ever purpose Generic version of Iterator rather than using Iterator amongst raw type.
2) If you lot desire to take away 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 dependent area to ConcurrentModificationException if Collection is modified afterwards Iteration started, but this alone happens inward instance of fail-fast Iterators.
4) There are ii types of Iterators inward Java, fail-fast together with fail-safe, banking concern jibe difference betwixt fail-safe together with 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 unopen to to a greater extent than differences betwixt Iterator together with ListIterator similar bidirectional traversing, which nosotros accept 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 amongst Example
Belum ada Komentar untuk "What Is Iterator Together With Listiterator Inwards Coffee Programme Amongst Example - Tutorial Code Sample"
Posting Komentar