Tutorials

Java Collection Framework


Introduction to Collection


Java Collection Framework gives programmer a smooth and efficient way of storing, retrieving and manipulating the data. Java Collections Framework becomes valuable because of the collection of interfaces and classes that it has to offer to the java programmer for performing various operations. 

 

The collection interface is present in java.util package

A good java developer must have a solid understanding of collection framework. Every java project will use one or more concepts from the collection framework.

 

To understand this framework, we first need to understand the framework hierarchy. And for that let's take  a look at the below diagram:

 

Collection Hierarchy in java

 

What we see from above diagram?

 

There is a collection interface at the top and is the root of the collection hierarchy.

And then we have sub-interfaces like Set, List, Queue, Deque, etc. 

Java platform doesn’t provide direct implementation for this root interface rather it provides implementation for more specific sub-interfaces like List, Set, etc.

 

Now we will go through each of the sub-interfaces one by one. 

 

List

List is an ordered Collection because the insertion order of the elements are maintained. Hence while retrieving the element you will get the elements in order they were inserted in the list. 

In List elements are stored at a specified index.

An element can be accessed using the index position.

List allows storage of duplicate elements.

 

A List can be traversed using iterator or ListIterator.

ListIterator can only be used with Lists.

The classes implementing list interface are:

  1. ArrayList 
  2. LinkedList
  3. Vector (now obsolete)

Set

Set is an unordered collection because insertion order of element may not be maintained.

Set does not allow storage of duplicate element. Each element in set is unique.

Set allows only one null value.

 

The classes implementing set interface are:

  1. HashSet
  2. LinkedHashSet
  3. TreeSet

Queue

The classes implementing queue interface are:

  1. Dequeue
  2. ArrayDeque

We have another important concept that is not a part of Collection interface hierarchy tree. It is Map interface.

 

Map

Map stores elements as Entry (Map.Entry).

Each Entry contains key and value.

In Map each key must be unique whereas values can be duplicate.

 

The classes implementing map interface are:

  1. HashMap
  2. LinkedHashMap
  3. TreeMap

Why Map is not a part of Collection in java?

We will learn in depth about each of the mentioned classes in upcoming chapters of this java tutorial.


Benefit of Collection Framework

  • Collection  enhances code reusability and inter-operability between unrelated API's
  • Collection provides lots of in-built methods which increases speed of the development process.
  • Programmers can rest assured while using Collection as the framework is already well tested and maintained.
  • Collection also provides concept of Generics which helps to restrict the type of Object that your collection can store. In case other data type is stored compile time error will occur.

 



Please Share this page
Views : 542
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!