JAVA COLLECTION FRAMEWORK

Ritik Goel
4 min readFeb 14, 2021

What are Collections in Java?

Collections are those containers which groups multiple elements into a single unit or module also known as multi value container. It provides such an architecture that helps in storing and manipulating a group of objects.
Using java collections we can perform several different operations example searching, sorting, insertion, manipulation, deletion etc.

A Java Collection Framework includes the following:

  • Interfaces
  • Classes
  • Algorithm
  1. Interfaces
    Interface in Java refers to the abstract data types. They allow Java collections to be manipulated independently from the details of their representation. Also, they form a hierarchy in object-oriented programming languages.
  2. Classes
    Classes in Java are the implementation of the collection interface. It basically refers to the data structures that are used again and again.
  3. Algorithm
    Algorithm refers to the methods which are used to perform operations such as searching and sorting, on objects that implement collection interfaces.

Commonly used methods of Collection interface

  1. public boolean add(object element): is used to insert an element in this collection.
  2. public boolean addAll(collection c): is used to insert the specified collection elements in the invoking collection.
  3. public boolean remove(object element): is used to delete an element from this collection.
  4. public boolean removeAll(Collection c): is used to delete all the elements of specified collection from the invoking collection.
  5. public boolean retainAll(Collection c): is used to delete all the elements of invoking collection except the specified collection.
  6. public int size(): return the total number of elements in the collection.
  7. public void clear(): removes the total no of element from the collection.
  8. public boolean contains(object element): is used to search an element.
  9. public boolean containsAll(collection c): is used to search the specified collection in this collection.
  10. public Iterator iterator(): returns an iterator.

Java Collection Framework Hierarchy

Collection Interface

Above image refers to different interfaces and classes. Now, lets see them in details one by one:

List

It is an interface that extends Collection Interface. It is the ordered collection of elements. It can also contain duplicates.

  • ArrayList
  • LinkedList
  • Vector
  1. Array List
    Array List is the implementation of List. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package.
 Syntax: ArrayList<Type> object = new ArrayList<Type>();
OUTPUT[Array, List]

2. Linked List
Linked List is a sequence of links which contains items. Each link contains a connection to another link. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays.

Syntax: Linkedlist<Type> object = new Linkedlist<Type>();
OUTPUT[Linked, List]

3. Vector
Vector is like the dynamic array which can grow or shrink its size. Unlike array, we can store n-number of elements in it as there is no size limit.

Syntax: Vector<Type> object = new Vector<Type>();
OUTPUT[Vector1, Vector2]

Queue

Queue in Java follows a FIFO approach i.e. it orders the elements in First In First Out manner. In a queue, the first element is removed first and last element is removed in the end.

Syntax: 
Queue<Type> q1 = new PriorityQueue();
Queue<Type> q2 = new ArrayDeque();

Priority Queue

The Priority Queue class implements the Queue interface. It holds the elements or objects which are to be processed by their priorities. Priority Queue doesn’t allow null values to be stored in the queue.

OUTPUTPriority
Priority

Sets

A Set refers to a collection that cannot contain duplicate elements. It is mainly used to model the mathematical set abstraction. Set has its implementation in various classes such as HashSet, TreeSet and LinkedHashSet.

HashSet

HashSet class implements Set Interface. It represents the collection that uses a hash table for storage. Hashing is used to store the elements in the HashSet. It contains unique items.

Syntax: HashSet<Type> object = new HashSet<Type>();
OUTPUTSet
Hash
HashSet

LinkedHashSet

LinkedHashSet class represents the Linked List implementation of Set Interface. It extends the HashSet class and implements Set interface. Like HashSet, It also contains unique elements. It maintains the insertion order and permits null elements.

Syntax: LinkedHashSet<Type> object = new LinkedHashSet<Type>();
OUTPUTHash
Set
LinkedHashSet

TreeSet

Java TreeSet class implements the Set interface that uses a tree for storage. Like HashSet, TreeSet also contains unique elements. However, the access and retrieval time of TreeSet is quite fast. The elements in TreeSet stored in ascending order.

Syntax: TreeSet<Type> object = new TreeSet<Type>();
OUTPUTHash
Set
TreeSet

Conclusion

The collection framework is the answer to the much needed central and unifying theme which is required to effectively handle the group of objects in Java framework. This is because as discussed already Collection Framework in java is a centralized as well as an amalgamated theme to store and stage-manage the group of objects in the Java frameworks. Java Collection Framework lets you have some really cool predefined classes along with the interfaces to efficiently handle the group of objects in it.

--

--