In how many ways you can retrieve the elements of a collection in Java?


You can retrieve the contents of a collection object in three ways −

Using for each loop

The foreach loop or enhanced for loop, which enables you to traverse the complete collection object sequentially.

Example

 Live Demo

import java.util.ArrayList;
public class RetrievingData {
   public static void main(String[] args) {
      ArrayList <String> list = new ArrayList<String>();
      //Instantiating an ArrayList object
      list.add("JavaFX");
      list.add("Java");
      list.add("WebGL");
      list.add("OpenCV");
      list.add("OpenNLP");
      list.add("JOGL");
      list.add("Hadoop");
      list.add("HBase");
      list.add("Flume");
      list.add("Mahout");
      list.add("Impala");
      System.out.println("Contents of the array list: ");
      for (String e: list)
         System.out.println(e);
   }
}

Output

Contents of the array list:
JavaFX
Java
WebGL
OpenCV
OpenNLP
JOGL
Hadoop
HBase
Flume
Mahout
Impala

Using Iterators

Java provides Iterator and ListIterator classes to retrieve the elements of the collection object.

  • The hasNext() method of these interfaces returns true if the collection object has next element else it returns false.

  • The next() methods of the Iterator and ListIterator returns the next element of the collection.

Using these two methods you can retrieve the contents from an iterator object.

  • Similarly, the previous() method of the ListIterator returns the previous element of the collection and hasPrevious() determines whether the current collection object has previous element.

You can get the Iterator or, ListIterator objects of a collection using the Iterator and ListIterator() methods.

Example

 Live Demo

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class RetrievingData {
   public static void main(String[] args) {
      ArrayList <String> list = new ArrayList<String>();
      //Instantiating an ArrayList object
      list.add("JavaFX");
      list.add("Java");
      list.add("WebGL");
      list.add("OpenCV");
      list.add("OpenNLP");
      list.add("JOGL");
      list.add("Hadoop");
      list.add("HBase");
      list.add("Flume");
      list.add("Mahout");
      list.add("Impala");
      System.out.println("Contents of the array list (first to last): ");
      Iterator it = list.iterator();
      while(it.hasNext()) {
         System.out.println(it.next());
      }
      System.out.println("Contents of the array list (last to first): ");
      ListIterator lit = list.listIterator();
      while(lit.hasNext()) {
         lit.next();
      }
      while(lit.hasPrevious()) {
         System.out.println(lit.previous());
      }
   }
}

Output

Contents of the array list (first to last):
JavaFX
Java
WebGL
OpenCV
OpenNLP
JOGL
Hadoop
HBase
Flume
Mahout
Impala
Contents of the array list (last to first):
Impala
Mahout
Flume
HBase
Hadoop
JOGL
OpenNLP
OpenCV
WebGL
Java
JavaFX

Using Enumeration

The Enumeration class contains a method named hasMoreElements() which returns true if the current object contains more elements after the current position (else it returns false).

If you call the nextElement() method of the Enumeration class returns the next element in the current enumeration object.

Using these two methods you can retrieve the contents of a collection object.

Example

 Live Demo

import java.util.Enumeration;
import java.util.Vector;
public class EnumerationExample {
   public static void main(String args[]) {
      //instantiating a Vector
      Vector<Integer> vec = new Vector<Integer>( );
      //Populating the vector
      vec.add(1254);
      vec.add(4587);
      vec.add(5211);
      vec.add(4205);
      vec.add(1124);
      vec.add(8115);
      //Retrieving the elements using the Enumeration
      Enumeration<Integer> en = vec.elements();
      while(en.hasMoreElements()) {
         System.out.println(en.nextElement());
      }
   }
}

Output

1254
4587
5211
4205
1124
8115

Updated on: 14-Oct-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements