The Iterator is used to iterate over all elements of a Collections inward Java. By Iteration, I mean, going over each chemical cistron stored inward the collection as well as optionally performing or thence performance e.g. printing value of an element, updating object or removing an object from Collection. Iterator was non portion of start Java release, as well as a similar class Enumeration was at that topographic point to render Iteration functionality. Iterator inward Java was introduced from JDK 1.4 as well as it provides an alternative to Enumeration, which is obsolete nowadays. An iterator is dissimilar to Enumeration inward ii original ways, first, Iterator allows a programmer to take elements from Collection during iteration.
Second, names are shortened as well as improved inward Iterator, past times the way, you lot tin dismiss run into the difference betwixt Iterator as well as Enumeration for to a greater extent than differences.
It's 1 of the oftentimes asked Java Interview questions. An iterator is an interface as well as enhanced to back upwardly Generic from Java 1.5 release.
Almost all pop collection implements Iterator, including ArrayList, LinkedList, as well as HashSet. hasNext() method of Iterator is used equally a status piece Iterating, as well as next() method genuinely returns an object, adjacent inward sequence maintained past times Collection itself.
In this Java programming tutorial, nosotros volition larn How to exercise Iterator inward Java past times coding Iterator representative as well as iterating over ArrayList.
Second, names are shortened as well as improved inward Iterator, past times the way, you lot tin dismiss run into the difference betwixt Iterator as well as Enumeration for to a greater extent than differences.
It's 1 of the oftentimes asked Java Interview questions. An iterator is an interface as well as enhanced to back upwardly Generic from Java 1.5 release.
Almost all pop collection implements Iterator, including ArrayList, LinkedList, as well as HashSet. hasNext() method of Iterator is used equally a status piece Iterating, as well as next() method genuinely returns an object, adjacent inward sequence maintained past times Collection itself.
In this Java programming tutorial, nosotros volition larn How to exercise Iterator inward Java past times coding Iterator representative as well as iterating over ArrayList.
How to exercise Iterator inward Java - Example
Using Iterator is in all likelihood simplest affair you lot volition larn inward Java programming. Every Collection, which implements Iterator interface, provides iterator() method which returns Iterator instance. This method comes from java.util.Iterator interface as well as render a type-safe Iterator.
Now, In lodge to start iterating or navigating, nosotros tin dismiss exercise piece loop as well as hasNext() method to cheque whether at that topographic point are to a greater extent than elements inward Iterator or not. In each run of piece loop, nosotros acquire access to 1 chemical cistron from Java Collection.
In this example, nosotros start impress the value of the chemical cistron as well as afterwards take it from Collection. So, at the terminate of our iteration, Java collection should last empty.
By the way, It's likewise worth knowing that at that topographic point are ii kinds of Iterator inward Java, fail-safe as well as fail-fast. fail-safe Iterator doesn't throw ConcurrentModificationException during iteration piece fail-fast does, if, Iterator realizes whatever structural alter inward Collection in 1 lawsuit Iteration begins. See the nice agency of creating List inward 1 line, This is the best agency to practice List if you lot know values inward advance.
Unfortunately, that List is fixed length List as well as doesn't back upwardly take operation, calling remove() volition lawsuit inward "Exception inward thread "main" java.lang.UnsupportedOperationException".
By the way, it's of import to recollect that this is non a read-only Collection, you lot tin dismiss nonetheless modify existing elements past times using set(index) method.
Now, In lodge to start iterating or navigating, nosotros tin dismiss exercise piece loop as well as hasNext() method to cheque whether at that topographic point are to a greater extent than elements inward Iterator or not. In each run of piece loop, nosotros acquire access to 1 chemical cistron from Java Collection.
In this example, nosotros start impress the value of the chemical cistron as well as afterwards take it from Collection. So, at the terminate of our iteration, Java collection should last empty.
By the way, It's likewise worth knowing that at that topographic point are ii kinds of Iterator inward Java, fail-safe as well as fail-fast. fail-safe Iterator doesn't throw ConcurrentModificationException during iteration piece fail-fast does, if, Iterator realizes whatever structural alter inward Collection in 1 lawsuit Iteration begins. See the nice agency of creating List inward 1 line, This is the best agency to practice List if you lot know values inward advance.
Unfortunately, that List is fixed length List as well as doesn't back upwardly take operation, calling remove() volition lawsuit inward "Exception inward thread "main" java.lang.UnsupportedOperationException".
By the way, it's of import to recollect that this is non a read-only Collection, you lot tin dismiss nonetheless modify existing elements past times using set(index) method.
CopyOnWriteArrayList supports fail-safe Iterator, to a greater extent than pop ones similar ArrayList or HashSet iterators are nonetheless fail-fast.
Further Learning
Java In-Depth: Become a Complete Java Engineer
5 departure betwixt HashMap vs Hashtable inward Java
Difference betwixt ConcurrentHashMap as well as HashMap inward Java
Difference betwixt Vector vs ArrayList inward Java
Further Learning
Java In-Depth: Become a Complete Java Engineer
5 departure betwixt HashMap vs Hashtable inward Java
Difference betwixt ConcurrentHashMap as well as HashMap inward Java
Difference betwixt Vector vs ArrayList inward Java
Thanks for reading this tutorial, if you lot similar this tutorial thence delight part amongst your friends as well as colleagues.
No comments:
Post a Comment