Open In App

Retrieving Elements from Collection in Java (For-each, Iterator, ListIterator & EnumerationIterator)

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Collection in Java 
Following are the 4 ways to retrieve any elements from a collection object: 
 

For-each

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());
  1. 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.
  2. 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.)
  3. 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

 


Last Updated : 19 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads