
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
In how many ways you can retrieve the elements of a collection in Java?
In Java, the collections framework provides various classes like ArrayList, HashSet, and LinkedList to store groups of elements. But once data is stored, how do you access or retrieve it?
Java provides multiple ways to retrieve elements from collections, depending on whether you're reading values, modifying them during traversal, or iterating forward or backward. You can retrieve the elements of a collection in four ways-
-
Using For-Loop
-
Using For-Each Loop
-
Uisng Iterator and ListIterator
-
Using Enumeration
Using For-Loop
A for loop retrieves elements by iterating over index values and accessing each element using the get(index) method of the collection.
import java.util.ArrayList; import java.util.List; public class ForLoopExample { public static void main(String[] args) { List < String > names = new ArrayList < > (); names.add("Manisha"); names.add("Amit"); names.add("Priya"); for (int i = 0; i < names.size(); i++) { System.out.println(names.get(i)); } } }
Let us compile and run the above program, this will give the following result -
Manisha Amit Priya
Using For-Each Loop
The foreach loop or enhanced for loop enables you to traverse the complete collection object sequentially.
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); } }
Let us compile and run the above program, this will give the following result -
Contents of the array list: JavaFX Java WebGL OpenCV OpenNLP JOGL Hadoop HBase Flume Mahout Impala
Using Iterator
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 a next element, else it returns false.
-
The next() method 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 a previous element.
You can get the Iterator or ListIterator objects of a collection using the Iterator and ListIterator() methods.
import java.util.ArrayList; import java.util.Iterator; import java.util.ListIterator; public class RetrievingData { public static void main(String[] args) { ArrayList list = new ArrayList (); //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()); } } }
Let us compile and run the above program, this will give the following result -
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.
import java.util.Enumeration; import java.util.Vector; public class EnumerationExample { public static void main(String args[]) { //instantiating a Vector Vector vec = new Vector (); //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 en = vec.elements(); while (en.hasMoreElements()) { System.out.println(en.nextElement()); } } }
Let us compile and run the above program, this will give the following result -
1254 4587 5211 4205 1124 8115