Using an Array to Store a Group of Objects
It is possible to store a group of objects into an array.Let us take an example,
Employee arr[]=new Employee[100];
This array can store 100 Employee objects.This can be achieved using a single loop as:
for(int i=0;i<100;i++)
{
arr[i] =new Employee[data];
}
So,here are few points:
Arrays can not grow dynamically.Its size is fixed and at run time its size can not increased or decreased.
We can not store different class objects into the same array.The reason is that an array can store only one data type of elements.
Adding the objects at the end of an array is easy but inserting and deleting in middle is difficult.
Collection Objects:
A collection object or a container is an object which can store a group of other objects.A collection object has a class called as Collection Class or Container Class .A group of collection classes are available in the package java.util and is called Collection Framework.
In fact ,collection object does not store the physical copies of other objects.Since other objects are already available in memory,storing another copy of them into the collection object would be a wasting of Memory.So JVM does not store the copies instead it simply stores the references of other objects into a collection object.
Does a collection object store copies of other objects or their references?
Ans: A collection object stores references of other objects.
All the collection classes in java.util package are the implementation classes of different interfaces as shown:
Interface type Implementation classes
Set<T> HashSet,LinkedHashSet
List<T> Stack,LinkedList,ArrayList,Vector
Queue<T> LinkedList
Map<K,V> LinkedList,HashMap,Hashtable
Sets: A set represents a group of element arranged just like an array.The set will grow dynamically when the elements are stored into it.A set will not allow duplicate elements.
Lists: Lists are like sets.They store a group of elements.But lists allow duplicate values to be stored.
Queues: A queue represents arrangement of elements in FIFO.This means that an element that is stored as a first element into the queue will be removed first from the queue.
Maps: Maps store elements in the form of key and value pairs.If the key is provided then its corresponding value can be obtained . of curse the keys should have unique values.
Note: In all cases the elements refer to objects only.This means we can not store primitive data type in the collection.
Can you store primitive data type into a collection?
Ans is NO,collections store only objects.
Retrieving Elements from Collections:
Using for-each loop
Using Iterator interface
Using ListIterator interface
Using Enumeration interface
What is the difference between Iterator and ListIterator?
Ans: Both are useful to retrieve elements from a collection.Iterator can retrieve the elements only in forward direction but in ListIterator can retrieve the elements in forward and backward direction also.
What is difference between Enumeration and Iterator?
Ans: Both are useful to get the elements from a collection.Iterator has methods whose names are easy to follow and Enumeration methods are difficult to remember.Also Iterator has an option to remove elements from the collection which is not available in Enumeration.