Java


Java Tutorial


Admission Enquiry Form


Collection Framework in Java



What is Collection Framework?

A group of collection classes is called a 'Collection Framework'.

A collection framework is a class library to handle groups of objects. Collection framework is implemented in java.util package.

Interface Type Implementation Classes
Set<T> HashSet<T> LinkedHashSet<T>
List<t> Stack<T> LinkedList<T> ArrayList<T> Vector<T>
Queue<T> LinkedList<T>
Map<K,V> HashMap<K,V> Hashtable<K,V>





What are Sets?

A set represents a group of elements arranged just like an array. The set will grow dynamically when the elements are stored into it.

A set will not allow duplicate elements.

The set is an interface available in the java.util package. The set interface extends the Collection interface.






What are Lists?

Lists are like sets. They store a group of elements. But lists allow duplicate values to be stored.

The List interface is found in the java.util package and inherits the Collection interface.

Since List is an interface, we cannot create objects from it.

Through the ListIterator, we can iterate the list in forward and backward directions. The implementation classes of the List interface are ArrayList, LinkedList, Stack, and Vector.






What are Queues?

A queue represents arrangement of elements in FIFO(First In First Out) order. This means that an element that is stored as a first element into the queue will be removed from the queue.

The interface Queue is available in the java.util package and does extend the Collection interface.

It is an ordered list of objects, where insertion of elements occurs at the end of the list, and removal of elements occur at the beginning of the list.






Classes that Implement Queue

Since the Queue is an interface, we cannot provide the direct implementation of it.

In order to use the functionalities of Queue, we need to use classes that implement it:

  • ArrayDeque
  • LinkedList
  • PriorityQueue





What are Maps?

Maps store elements in the form of key and value pair. If the key is provided then its corresponding value can be obtained. Of course, the keys must have unique values.

A Map doesn't allow duplicate keys, but you can have duplicate values.






Points to Remember:

In all the cases the 'elements' refer to 'objects' only. This means we cannot store primitive data types in the collection objects.

We can store only objects since the main aim of collections is to handle objects only, not the primitive data types.