
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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
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
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
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
- Related Articles
- How many ways can get the instance of a Class class in Java?
- In how many ways we can concatenate Strings in Java?
- How many ways a String object can be created in java?
- How many ways can we read data from the keyboard in Java?
- Rotate elements of a collection in Java
- In how many ways can I compare Strings in Java explain with example?
- How many ways to iterate a TreeSet in Java?
- How many ways to iterate a LinkedList in Java?
- In how many ways we can convert a String to a character array using Java?
- How to retrieve all the documents from a MongoDB collection using Java?
- In how many ways can we split a string in JavaScript?
- How to retrieve documents from a collection in MongoDB?
- Retrieve a set of Map.Entry elements from a HashMap in Java
- Retrieve environment variables with Java Map Collection
- How many ways are there to register a driver in Java?
