Prerequisite: Collection in Java
Following are the 4 ways to retrieve any elements from a collection object:
For each loop is meant for traversing items in a collection.
// Iterating over collection 'c' using for-each for (Element e: c) System.out.println(e);
We read the ‘:’ used in for-each loop as “in”. So loop reads as “for each element e in elements”, here elements are the collection which stores Element type items.
Note:In Java 8 using lambda expressions we can simply replace for-each loop with
elements.forEach (e -> System.out.println(e) );
Using Cursors
Cursor is an interface and it is used to retrieve data from collection object, one by one. Cursor has 3 types, which are given below:
Iterator Interface: Iterator is an interface provided by collection framework to traverse a collection and for a sequential access of items in the collection.
// Iterating over collection 'c' using iterator for (Iterator i = c.iterator(); i.hasNext(); ) System.out.println(i.next());
- It has 3 methods:
- boolean hasNext(): This method returns true if the iterator has more elements.
- elements next(): This method returns the next elements in the iterator.
- void remove(): This method removes from the collection the last elements returned by the iterator.
- ListIterator Interface: It is an interface that contains methods to retrieve the elements from a collection object, both in forward and reverse directions. This iterator is for list based collections.
It has following important methods:- booleanhasNext(): This returns true if the ListIterator has more elements when traversing the list in the forward direction.
- booleanhasPrevious(): This returns true if the ListIterator has more elements when traversing the list in the reverse direction.
- element next(): This returns the next element in the list.
- element previous():This returns the previous element in the list.
- void remove(): This removes from the list the last elements that was returned by the next() or previous() methods.
- int nextIndex() Returns the index of the element that would be returned by a subsequent call to next(). (Returns list size if the list iterator is at the end of the list.)
- int previousIndex() Returns the index of the element that would be returned by a subsequent call to previous(). (Returns -1 if the list iterator is at the beginning of the list.)
- EnumerationIterator Interface: The interface is useful to retrieve one by one the element. This iterator is based on data from Enumeration and has methods:
- booleanhasMoreElements(): This method tests if the Enumeration has any more elements or not .
- element nextElement(): This returns the next element that is available in elements that is available in Enumeration
Related Articles:
Iterators in Java
Iterator vs Foreach In Java
This article is contributed by Nishant Sharma. If you like Lazyroar and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above