Collections Framework

This tutorial explains complete collections framework in java with examples. We will explain the problem with arrays first, before going to see what is collections framework.

What is an Array?

Array is fixed in size and can hold only homogeneous elements.

Problem with Arrays:

1) Fixed in Size: Arrays are fixed in size

int [] number = new int[10];

Here we declared number array as 10. We cannot change size of an array after declaring.

2) Homogeneous:  Arrays can hold only homogeneous elements. We will get compilation error, if we try to assign an   array of one type of object with different data type of an object.

Employee [] employee = new Employee [100];

employee [0] = new Employee();

employee [1] = new Employee();

employee [2] = new Employee();

employee [3] = new Subscriber(); // We will get compilation error.

 Exception in thread “main” java.lang.Error: Unresolved compilation problem:

       Type mismatch: cannot convert from Subscriber to Employee

3) No API:  We don’t have any API for arrays and arrays don’t follow any data structures. For each task programmer has to write his own logic. Ex: Adding elements, retrieving elements and Iterating elements…Etc.

Sun people created Collections framework to solve the above problems.

Advantages with Collections framework:

1)  Re-sizable:  Collections are re sizable.

2) Homogeneous & Heterogeneous: Collections can hold both homogeneous and heterogeneous data elements.

3) API support:  Each data structure have its own collections class with existing API. Programmers no need to write any logic on data operations.

Arrays vs. Collections:

Arrays Collections
Fixed in size. Resizable in nature.
Can hold only homogenous elements.( Primitives and Objects of same type). Can hold both homogenous and heterogeneous elements. (No primitives ,Only objects of same and different type).
Not recommended to use in memory point of view. Recommended to use in memory point of view.
Recommended to use in performance point of view. Not recommended to use in performance point of view.
No underlying data structure. So no API support. Underlying data structure for each collection class with API support.

Collections framework:

It provides several classes and interfaces which can be used to represent group of objects as a single unit.

Collection:  collection is nothing but group of objects representing as a single unit.

Interfaces in Collections framework:

1) Collection: java.util.Collection :

  • Collection interface is the root interface in collection hierarchy.
  • It represents group of objects as a single unit.
  • All Collection implementation classes (ArrayList ,HashSet , Queue …Etc. ) typically implements Collection interface indirectly through their sub interfaces.

    Collection Vs Collections:

    Collection is an Interface, and used to represent group of objects as single unit.

    Collections is an utility class which consists several static utility methods which operate on or return collections.

2) List: java.util.List :

public interface List<E> extends Collection<E> 

  • List interface extends Collection interface. So List is child interface of Collection.
  • List is an ordered collection (insertion order is preserved).
  • We can access the elements in list by using index.
  • List allows duplicate elements.
  • List allows multiple null elements.
  • ArrayList (Since Java 1.2), LinkedList (1.2) and Vector (1.0) (Stack (1.0) extends Vector) are the implementation classes of List interface.

Click below link to see more information on List implementation classes

3) Set: java.util.Set :

 public interface Set<E> extends Collection<E> 

  • Set interface extends Collection interface. So Set is child interface of Collection.
  • Set is an Unordered collection (insertion order is not preserved).
  • Set contains no duplicate elements.
  • Set can contain at most one null element.
  • HashSet (1.2) is the implementation class of Set interface.
  • LinkedHashSet(1.4) extends HashSet. 

Click below link to see more information on Set implementation classes

 4) SortedSet: java.util. SortedSet:

public interface SortedSet <E> extends Set<E> 

  • SortedSet interface extends Set interface. So SortedSet is child interface of Set.
  • Use SortedSet, if you want to represent group of individual objects in some sorting order. 

5) NavigableSet: java.util.NavigableSet:

public interface NavigableSet<E> extends SortedSet<E>

  • NavigableSet interface extends SortedSet interface. So NavigableSet is child interface of SortedSet.
  • Provides methods for navigation purpose.
  • NavigableSet has been added in Java 1.6
  • TreeSet(1.2) extends NavigableSet

Click below link to see more information on TreeSet

Click below link to see Comparison table for Set implementation classes.  

6) Queue:java.util.Queue:

public interface Queue<E> extends Collection<E>

  • Child interface of Collection
  • Queue is used to hold elements prior to processing.
  • Queue has been added in Java 1.5
  • PriorityQueue class implements Queue interface
  • BlockingQueue interface extends Queue interface
  • PriorityBlockingQueue and LinkedBlockingDeque are the implementation classes of BlockingQueue interface.

Click below link to see more information on PriorityQueue

7) Map: java.util.Map

  • Map is not a child interface of Collection.
  • Map represents objects as key-value pairs.
  • Key &Value of a Map should be Object only.
  • Map not allows duplicate key’s but allows duplicate values.
  • HashMap (1.2), WeakHashMap (1.2), IdentityHashMap (1.4), LinkedHashMap (1.4) are the implementation classes of a Map interface.
  • HashTable class implements Map interface but extends Dictionary abstract class. 

Click below links to see more details on Map implemented classes.

8) SortedMap: java.util.SortedMap

public interface SortedMap<K,V> extends Map<K,V> 

  • Child interface of Map.
  • Used to represent Map values in some sorting order.
  • Sorting happens with the keys.
  • NavigableMap interface extends SortedMap interface.
  • TreeMap Class implements NavigableMap interface.

Click below link to see more information on TreeMap

Leave a Reply

Your email address will not be published. Required fields are marked *