Data Retrieving Technique in Collection Framework
Data Retrieving Technique form Collection Framework
Technique to retrieve elements from Collection object
Java support following technique to retrieve the elements from any collection object.
- foreach loop
- Iterator interface
- ListIterator interface
- Enumeration interface
foreach
It is similar to for loop used to retrieve elements of collection objects (until the last element)
Syntax
for(datatype variable:collection-object) { ..... ..... }
Iterator interface
It is one of the predefined interface present in java.util.* package. The purpose of this interface is that to extract or retrieve the elements of collection variable only in forward direction but not in backward direction. By default an object of iterator is pointing just before the first element of any collection framework variable.
Methods of Iterator Interface
- public boolean hasNext()
- public object next()
ListIterator interface
It is one of the predefined interface present in java.util.* package. The ListIterator interface object is always used for retrieving the data from any collection framework either forward direction or backward direction or in both direction. Like Iterator interface object, ListIterator interface object is pointing just before the first element of any collection framework variable.
Methods of ListIterator Interface
- public boolean hasNext()
- public object next()
- public boolean nestPrevious()
- public object previous()
Enumeration interface
It is one of the predefined interface and whose object is always used for retrieving the data from any legacy collection framework variable only in forward direction but not in backward direction. Like Iterator interface object, Enumeration Interface object is pointing just before the first element of any legacy collection framework variable.
The functionality of Enumeration is more or less similar to Iterator Interface but Enumeration Interface object belongs to synchronized and Iterator Interface object belong to non-synchronized.
Methods of Enumeration Interface
- public boolean hasMoreElements()
- public object nextElement()