Java Collections

In this tutorial, we will learn about what is the Collections, types of collections in Java and walkthrough on list of classes available based on its type.

What is a "Java collections"?

Java Collections is a framework which provides set of interfaces and classes to provide, main goal of the collections is to group several objects and perform operations over a grouped object. Grouping of objects means storing of objects in to the memory of JVM,

While storing objects, collections follow an organized way of choosing an appropriate data structure. Each class of the collection follows a single data structure it can be either linear or non-linear data structure.

Types of collections

Collections are broadly classified as follows.

  1. List based collections
  2. Set based collections
  3. Queue based collections

List based collections

List based collections allows to store elements(Objects) in a sequential order of index and iterating elements also done in order which they added, the following are the features of List collections.

  1. Linear data structures are used.
  2. Dynamic size in nature(N numbers of elements can be added to form a group).
  3. Allows random access of elements based on index of element.
  4. Allows duplicate elements.
  5. Size of the data structure is not fixed.
  6. All classes under list implements java.util.List interface.

Based on the type of the linear data structure and its behaviour, list collections are further divided into as follows.

  1. ArrayList
  2. LinkedList
  3. Vector
  4. Stack

ArrayList:

It uses Array based data structure, which will have initial capacity. Capacity will grow when number of elements reached to its initial capacity by creating a new array with larger size and copies the existing elements from old array to new array .

LinkedList:

As the name implies, it uses Linked List data structure and contains initial capacity. When number of elements reached to its initial capacity will not copy the elements to other data structure because internally it uses node based linkage.

Vector:

It uses Array based data structure like ArrayList but all the operations of Vector are thread safe. Only one operation can be performed at a time performance of the Vector is poor as compared to the ArrayList.

Stack:

It extends Vector class and provides an additional functionality as last in first out. Additional methods of stack as compared to vector are push, pop and peek.

Set based collections

As the name implies set based collections will not allow duplicate elements, only one null element can be added and the following are the feature of set collections.

  1. Uses non-linear data structure.
  2. Random access is not possible.
  3. Uses hash code and equals method of element class to identify duplicates.
  4. Only one null element can be inserted
  5. Dynamic size in nature.
  6. All classes under set implements java.util.Set interface

Only two classes are available for set based collections.

  1. HashSet
  2. LinkedHashSet

Only difference from HashSet to LinkedHashSet is insertion order maintenance, where LinkedHashSet maintains it and HashSet not maintains insertion order.

In LinkedHashSet next iterating element can be predicted but in case HashSet not as insertion order is not maintained by it.

Queue based collections

As the name implies, it follows the order of first in and first out(FIFO). It uses the linear data structure and the following features of the Queue.

  1. Follows the order of insertion.
  2. Removal takes place rear end whereas adding takes in the front end.
  3. Exit will happen in the order of first in and first out.
  4. Deletion takes place only at the front end.

The following is only one class implements it and one interface extends it from the collections.

  1. PriorityQueue is a class implements Queue
  2. Deque is an interface which extends Queue

Conclusion:

In this tutorial, we have covered what is collections and high-level understanding on classes and interfaces which are part of collections.