What Is Iterator As Well As Listiterator Inwards Coffee Plan Amongst Illustration - Tutorial Code Sample

What Is Iterator As Well As Listiterator Inwards Coffee Plan Amongst Illustration - Tutorial Code Sample - Hallo sahabat BEST LEARNING JAVA, Pada Artikel yang anda baca kali ini dengan judul What Is Iterator As Well As Listiterator Inwards Coffee Plan Amongst Illustration - Tutorial Code Sample, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel core java, Artikel java collection tutorial, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : What Is Iterator As Well As Listiterator Inwards Coffee Plan Amongst Illustration - Tutorial Code Sample
link : What Is Iterator As Well As Listiterator Inwards Coffee Plan Amongst Illustration - Tutorial Code Sample

Baca juga


What Is Iterator As Well As Listiterator Inwards Coffee Plan Amongst Illustration - Tutorial Code Sample

Iterator inwards Java is aught but a traversing object, made specifically for Collection objects similar List and Set. nosotros convey already aware virtually dissimilar sort 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 thus 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 inwards Java which nosotros volition inwards this Java Iterator tutorial.


What is Iterator inwards 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 virtually specific implementation of that collection it. Basically List as well as laid collection provides the iterator. You tin plow over the sack instruct Iterator from ArrayList, LinkedList, as well as TreeSet etc. Map implementation such equally HashMap doesn’t supply Iterator directory but you lot tin plow over the sack instruct at that topographic point keySet or Value Set as well as tin plow over the sack 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 adjacent chemical component subdivision inwards the collection until the hasNext() method render true. Its ever recommended to telephone telephone hasNext() method earlier calling next() method to avoid java.util.NoSuchElementException: Hashtable Iterator

remove(): method take away the concluding chemical component subdivision render past times the iterator this method solely calls i time per telephone telephone to next().



How to create Iterator inwards Java:

 nosotros convey already aware virtually dissimilar sort of traversing methods similar  What is Iterator as well as ListIterator inwards Java Program alongside Example - Tutorial Code SampleEvery collection classes provides an iterator() method that returns an iterator to the outset of the collection. By using this iterator object, nosotros tin plow over the sack access each chemical component subdivision inwards 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 upwardly a loop that makes a telephone telephone 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 inwards Java
Here is consummate code instance of How to purpose Iterator inwards Java. This Java programme creates Hashtable, populate it alongside dummy information as well as than uses Iterator to traverse Map as well as prints substitution 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 inwards 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 inwards 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 inwards Java, hither Map implementation is Hashtable
        Iterator<Entry<Integer, String>> i= stockSet.iterator();
        System.out.println("Iterating over Hashtable inwards Java");
       
        //Iterator begins
        while(i.hasNext()){
            Map.Entry<Integer,String> m=i.next();
            int substitution = m.getKey();
            String value=m.getValue();
            System.out.println("Key :"+key+"  value :"+value);

        }
        System.out.println("Iteration over Hashtable finished");
    }
}

Output:
Iterating over Hashtable inwards 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 inwards damage of extra method remove () it supply us for modify the collection which is non available inwards enumeration along alongside that it is to a greater extent than secure it doesn’t allow around other thread to modify the collection when around some other thread is iterating the collection as well as throws concurrentModificationException. Along alongside this method mention are too rattling convenient inwards Iterator which is non major divergence but brand purpose of iterator to a greater extent than easy.


What is List Iterator inwards 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 plow over the sack obtain ListIterator from all List implementation including ArrayList as well as LinkedList. ListIterator doesn’t boot the bucket on electrical current index as well as its electrical current seat is determined past times telephone telephone to next() or previous() based on management of traversing.


Important betoken virtually Iterator inwards Java:

1) Iterator inwards Java back upwardly generics as well as thus ever purpose Generic version of Iterator rather than using Iterator alongside 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 whatsoever ConcurrentModificationException.

3) Iterating over collection using Iterator is dependent area to ConcurrentModificationException if Collection is modified afterwards Iteration started, but this solely happens inwards instance of fail-fast Iterators.

4) There are 2 types of Iterators inwards Java, fail-fast as well as fail-safe, banking concern lucifer difference betwixt fail-safe as well as fail-fast Iterator for to a greater extent than details.

5) List collection type too supports ListIterator which has add() method to add together elements inwards collection piece Iterating. There are around 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 inwards Java alongside Example


Demikianlah Artikel What Is Iterator As Well As Listiterator Inwards Coffee Plan Amongst Illustration - Tutorial Code Sample

Sekianlah artikel What Is Iterator As Well As Listiterator Inwards Coffee Plan Amongst Illustration - Tutorial Code Sample kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel What Is Iterator As Well As Listiterator Inwards Coffee Plan Amongst Illustration - Tutorial Code Sample dengan alamat link https://bestlearningjava.blogspot.com/2019/09/what-is-iterator-as-well-as.html

Belum ada Komentar untuk "What Is Iterator As Well As Listiterator Inwards Coffee Plan Amongst Illustration - Tutorial Code Sample"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel