Java Collection : Overview

A framework is a set of interfaces and classes which contain predefined architecture. A framework is a collection of classes such that all the classes perform the same kind of task. 

The Java collection framework provides a predefined architecture to manipulate and store a group of objects.

Java Collections provide a predefined operation that you can perform on data such as insertion, deletion, manipulation, and deletion.

Why the need for Java Collection over the Arrays?

  • An array cannot grow and shrink dynamically.
  • An array doesn’t have type safety.
  • Implementing data structure from scratch is difficult.

Benefits of java Collections Framework

  1. Reduce Development Efforts – Collection provides predefined methods used to manipulate and iterate the data. So we concentrate on business logic rather than providing implementation, manipulation and integration of the data. 
  2. Reusability and Interoperability –  It is easy to learn as it works in a similar manner and interoperability.
  3. Increase Quality – Using a collection of predefined classes is well tested so the number of loops holes is negligible as compared to the user-developed data structure.
  4. Abstraction – Interface provides abstraction as we just to know the name of methods to implement the data structure rather than caring about the implementation of methods.

Hierarchy of Collection Framework

Java.util contains a collection framework as given below architecture of implementation.

Methods in collection Framework

S.NoMethodsDescription
1public boolean add(E e)Insert element return true 
2public boolean addAll(Collection<? extends E> c)Insert an object into another object 
3public boolean remove(Object element)Removes element
4public boolean removeAll(Collection<?> c)thus, Remove a set of the element present in one object from another object  
5default boolean removeIf(Predicate<? super E> filter)Remove the element with a filter, for example remove element if age above age 60 
6public boolean retainAll(Collection<?> c)Delete all different elements that are not present specified collection
7public int size()Return number of elements in the collection
8public void clear()Remove all element from the collection
9public boolean contains(Object element)Return true if element found in collection else false. It is used to search element the collection
10public boolean containsAll(Collection<?> c)Return true if all elements of one collection are present in a specified collection. It is used to search elements of the collection.
11public Iterator iterator()It is to manipulate the data structure
12public Object[] toArray()Convert collection into an array
13public <T> T[] toArray(T[] a)Convert collection into an array but at the runtime
14public boolean isEmpty()Returns true if there is no element in data struture else return false.
15default Stream<E> parallelStream()returns a  parallel Stream with the data structure as its source.
16default Stream<E> stream()returns a sequential Stream with the data-structure as its source.
17default Spliterator<E> spliterator()a Spliterator over the specified elements in the collection.
18public boolean equals(Object element)return  if the object is the same 
19public int hashCode()returns the hash code number

Iterable Interface

The iterable interface is the root interface in the collection.

Iterator<T> iterator()  

List Interface 

List interface is the child interface of the Collection interface. The list is implemented by classes of vector, stack, Arraylist, and stack.

ArrayList 

This implements the list interface. It is a kind of dynamic array. In this duplicate data is stored. In this insertion order and is non-synchronized. We can access the element in ArrayList randomly.

LinkedList

This is a kind of dynamic array. In this duplicate data is stored. The insertion order is maintained and is non-synchronized. We cannot access the element in ArrayList. This is used in which manipulation of elements is required to be fast because no shifting is required.

Vector

This uses a dynamic array to store the data elements. It is synchronized. 

Stack

It implements the last first-out data structure. It uses methods like push, pop, peek.

Queue Interface

It maintains a first-in-first-out order. 

Set Interface 

This data structure does not allow to store the duplicate elements. Only one null element can be stored.

Conclusion on Java Collection

Collection in java allows you to use almost all commonly used data structure like list, vector, ArrayList, LinkedList,  Set, Map, queue, stack, etc and also provide with the methods to use in implementing the data structure like manipulation, searching, sorting, insertion, deletion, etc. This helps the user to code fast, efficiently, and increase code reusability and readability. 

written by: Charchil Gupta

Reviewed by: Soutik Maity

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

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