TUTORIALS

Collections in Java.

Collections Framework in Java

What is Collection in Java?

Collection:  A Collection is a group of individual objects as a single entity.  If you want to represent a group of individual objects as a single entity then we should go for Collection.

e.g. Collection of pens. The collection is also known as Container. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Using Collection we can perform many operations such as searching, sorting, insertion, manipulation, deletion etc.

What Is a Collections Framework?

collections framework is a unified architecture for representing and manipulating collections. If you want to represent a group of individual objects as a single entity then we required several interfaces or classes and these classes and interfaces are stored in Collection Framework. The java collections framework contains many classes, interfaces and algorithms which help in storing and processing the data efficiently.

This collection Framework provides many interfaces such as List, Queue, Deque, Set etc. and classes are ArrayList, LinkedList, PriorityQueue, HashSet, TreeSet, Vector etc.

All the classes and interfaces of collections framework are stored in java.util package.

Hierarchy of Collections Framework:

Now see the hierarchy of Collections Framework.

The following are the description of core collection interfaces:

  • Iterator: An Iterator is an interface that enables you to traverse the elements in forward direction only.
  • Collection: – In general Collection interface is considered as the root interface of Collection Framework. A collection represents a group of objects known as its elements. Collection interface defines most common methods which are applicable for any collection objects. Some common methods of Collection interface are add(), addAll(), size(), remove, removeAll, iterator() etc. And note that there is no concrete class which implements collection interface directly.

 

  • List: – The List interface is the child interface of Collection. It is also known as Ordered Collection or Sequence. If we want to represent a group of individual objects as single entity where duplicates are allowed and insertion order is preserved then we should go for List.

 

  • Set: – It is the child interface of Collection interface. The Setinterface contains only methods inherited from Collection If we want to represent a group of individual objects as single entity where duplicates are not allowed and insertion order is not preserved then we should go for Set. Set interface contains unique values only.

 

  • .SortedSet: – It is the child interface of Set. SortedSet extends Set interface. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls. If we want to represent a group of individual objects as single entity where duplicates are not allowed but all the objects should be inserted according to some sorting order then we should go for SortedSet.

 

  • NavigableSet: – It is the child interface of SortedSet. This interface defines several methods for navigation purpose.

 

  • Queue: – If we want to represent a group of individual objects prior to processing then we should go for Queue. Queue interface is used to hold multiple elements prior to processing. It follows the FIFO principle. In FIFO, first element is removed first and last element is removed at last.

 

  • Deque: – Deque is the subtype of queue interface. Deque interface is also used to hold multiple elements prior to processing. This can be used both as FIFO (first-in, first-out) and LIFO (last-in, first-out). In a deque all new elements can be inserted, retrieved and removed at both ends. Deque is stands for Double ended queue.

 

  • Map: – Map is not a child interface of Collection. This maps keys to values. If we want to represent group of individual objects as a key value pairs then we should go for Map.Map follows the Ascending order to store the elements. Here each key and value pair is known as an entry. A Mapcontains only unique keys, each key can map to at most one value.

 

  • SortedMap:-SortedMap is used for naturally ordered collections of key/value pairs, such as dictionaries and telephone directories. It is a child interface of Map. If we want to represent group of key value pairs according to some sorting orders of keys then we should go for SortedMap.

 

  • NavigableMap:- It is the child interface of SortedMap. It defines several utility methods for navigation purposes.

 

Next Topic:-  Method Overloading vs Method Overriding.