Collection framework in JAVA

Home  >  Articles  >  Collection framework in java

Collection framework in java

Shakshi Sehrawat

Updated on 03rd July, 2024 , 6 min read

Collection in java 

The collection in Java is a framework that gives an architecture that stores and manipulates the group of objects. Java collection means a single unit of object. Java collection can archive all the operations that you perform on data such as searching, sorting, insertion, manipulation or delectation. Java framework provides many interfaces (set, queue, list, deque) and classes ( ArrayList, Vector, Likedlist, PriorityQueue, HashSet, LikedHastSet, Treeset)

In simpler terms, we can define it as: 

Any group of individual objects that can be represented as a single unit is called a Java collection of objects. In Java, a separate framework named the “Collection framework” has been defined in JDK2.1 which holds all the Java collection classes. 

What do you learn in the Java collection? 

 The names and details of the Java collection are given below: 

Names

Details

List Interface

Abstract List Class 

Abstract Sequential List Class

Array List

Vector Class

Stack Class 

LikedList Class

SortedSet Interface

NavigableSet Interface 

TreeSet

ConcurrentSkipListSet Class 

Map Interface 

Sorted Map Interface 

NavigableMap Interface 

ConcurrentMap Interface

HashMap Class

IdentityHashMap Class

HashTable Class

Properties Class 

Set Interface 

Abstract Set Class 

EnumSet Class

ConcurrentHashMap Class

LikedHashSet Class 

HashSet Class

Deque Interface 

BlockingDeque Interface 

ConcurrentLiked Deque Class 

ArrayDeque Class

Queue Interface

Blocking Queue Interface 

Abstract Queue Class

Priority Queue Class 

Priority Blocking Queue Class

Delay Queue Class 

LikedBlocking Queue Class

Liked Transfer Queue

Also see: Abstract Class in Java

What is the framework in Java? 

A framework in java is a set of classes and interfaces.It  provides a ready-made architecture which helps in implementing a new feature or a class, However, an optimal object-oriented design always includes a framework with a collection of classes such that all classes perform the same kind of task. 

  • It provides readymade architecture 
  • It represents a set of classes and interfaces
  • It is optimal 

What is a collection framework? 

The collection interface is the interface which is implemented by the collection framework. It is a declaration of the methods that every collection should have. In simpler words, we can say that the collection interface builds the foundation on which the collection framework depends. 

  • The collection framework signifies an architecture for storing and manipulating a group of objects which are: 
  1. Interface and its implementation, i.e classes
  2. Algorithms 

Interfaces that stretch the Java Collection Interface 

The following interfaces are present in the framework:

Iterable Interface 

  • The Iterable interface is the main interface for all the collection classes. This Collection interface extends the Iterable interface and helps all the subclasses to the Collection interface so they implement the Iterable interface.
  • It is the one abstract method. The main function of the Iterable interface is to provide an iterator for all the collections. 

List Interface 

  • The list interface is like the child interface of the collection interface. This prevents a list-type data structure that can store the ordered collection of objects. It can have various copied values. 
  • List Interface is applicable to all the classes ArrayList, stack,Vector, and Likedlist. 

Collection Interface 

  • This interface builds massively the iterable interface and is applicable to all the classes in this framework. This interface has all the basic methods which every collection must have, for example: adding the data into the collection, deleting the data, or erasing the data, etc. 
  • In other words, it can be said that the Collection interface makes up the foundation on which the collection framework lays.

Methods of collection interface 

There are many methods proclaimed in the collection interface, they are as follows:

Method

Description 

add(Object).

In this method it is used to add an object to the collection.

addAll(Collection c)

It is used to add all the elements in the given collection to this collection.

clear()

This method helps in  removing all of the elements from this collection.

contains(Object o)

This method helps in returning true if the collection contains the specified element.

containsAll(Collection c)

This method keeps returns true if the collection holds all of the elements in the given collection.

equals(Object o)

In this method it compares the specified object with this collection for equality.

hashCode()

This process is used to return the hash code value for this collection.

isEmpty()

 It returns true if this collection contains no elements.

iterator()

It  returns an iterator over the elements in this collection.

parallel stream()

This method returns a parallel Stream with this collection as its source.

remove(Object o)

It removes the given object from the collection and if there are any copied values, then this method erases the first occurrence of the object.

ArrayList

The ArrayList class implements the List interface. It uses a dynamic array to store the duplicate elements of different data types. The ArrayList class keeps up  the insertion order and is uncoordinated. The elements stored in the ArrayList class can be randomly accessed. The diameter of an ArrayList is increased by itself if the collection grows or reduces if the objects are removed from the collection.

LikedList 

LinkedList implements the Collection interface. It uses a doubly linked list internally to store the elements. It can store the duplicate elements. It continues the insertion order and it is uncoordinated. The elements are linked using pointers and addresses. Each element is known as a node.

Vector

A vector provides dynamic arrays in Java. Though it may be slower than standard arrays it can be helpful in programs where lots of manipulation in the array is needed. Vector uses a dynamic array for various purposes like storing the data elements. It is similar to ArrayList. However, It is synchronized and contains many methods that are not part of the Collection framework.

Stack 

The stack is the subclass of Vector. It implements the last-in-first-out data structure, i.e., Stack. The class is based on the basic principle of LIFO which stands for last-in-first-out. In addition to the basic push and pop operations, the class gives us the three main functions:

 1 empty

2 search

 and 3 peeks. 

These classes can be viewed as the subclass of the Vector.

Queue Interface 

The queue interface maintains the first-in-first-out order. It can be defined as an ordered list used to hold the elements about to be processed. This interface is dedicated to storing all the elements where the order of the elements matters. Therefore, the person whose request arrives first in the queue gets the ticket.

Deque Interface 

The Deque interface extends the Queue interface. In Deque, we can erase and add the elements from both sides. Deque stands for the double-ended queue which lets us perform the operations from both ends. This interface extends the queue interface. The class which implements this interface is ArrayDeque. The ArrayDeque class implements the Deque interface; it can instantiate a deque object with this class.

ArrayDeque Interface 

The ArrayDeque class which is implemented in the collection framework provides us with a way to apply a resizable array. This kind of array grows and allows the users to add or erase an element from both sides of the queue. The ArrayDeque class executes the Deque interface. It facilitates the use of the Deque. Unlike queue, we can add or delete the elements from both ends.ArrayDeque is faster than ArrayList and Stack and has no capacity restrictions.

Set Interface 

A set is an unorganized collection of objects whose copied values cannot be stored. This collection is used when you wish to avoid the duplication of the objects and wants to store only the unique objects. The set interface is applicable on various classes like HashSet, LinkedHashSet, TreeSet,etc. Set Interface in Java is present in Java. util package. It extends the Collection interface. It represents the unorganized set of elements which don't allow you to store the copied items.

SortedSet Interface 

The SortedSet interface is very likewise to the set interface. The major difference that can be viewed is the interface has extra methods which helps in  maintaining the order of the elements. This interface enlarges the set interface and it can be used to handle the data that needs to be sorted. The elements of this interface are arranged in the ascending (increasing) order. The SortedSet provides extra methods that enable the natural ordering of the required elements.

Map Interface 

A map is a data structure which supports the key-value pair for the mapping of data. A map is useful if there is data and we wish to perform operations on the basis of the key.This interface doesn’t support duplicate keys because the same key cannot do multiple mappings, but it allows to copy values in different keys.

Advantages of Java collection framework 

The advantages of a collections framework are as follows:

  • It helps in reducing  programming efforts and it also provides data structures and algorithms so one doesn't have to write them yourself.
  • It helps in increasing performance and it  provides high-performance implementations on the  data structures and algorithms.
  • Consistent API: The API is a basic set of interfaces for example Collection, Set, List, or Map, of all the classes that implement these interfaces will have some common set of methods.
  • It reduces programming efforts. 
  • Due to the implementation of useful data structures and algorithms it Increases program speed and quality.

Also Read: Difference between Python and Java

Frequently Asked Questions

What is meant by collection in java?

The collection in Java is a framework that gives an architecture that stores and manipulates the group of objects. Java collection means a single unit of object. Java collection can archive all the operations that you perform on data such as searching, sorting, insertion, manipulation or delectation.

What is meant by framework in java ?

A framework in java is a set of classes and interfaces.It provides a ready-made architecture which helps in implementing a new feature or a class, However, an optimal object-oriented design always includes a framework with a collection of classes such that all classes perform the same kind of task.

What is the collection framework?

The collection interface is the interface which is implemented by the collection framework. It is a declaration of the methods that every collection should have. In simpler words, we can say that the collection interface builds the foundation on which the collection framework depends.

Explain what is meant by Array List?

The ArrayList class implements the List interface. It uses a dynamic array to store the duplicate elements of different data types. The ArrayList class keeps up the insertion order and is uncoordinated. The elements stored in the ArrayList class can be randomly accessed.

List a few advantages of collection in java?

It helps in reducing programming efforts and it also provides data structures and algorithms so one doesn't have to write them yourself. It helps in increasing performance and it provides high-performance implementations on the data structures and algorithms.

Check Eligibility   Free 1:1 Counselling