Collections in Java

By | July 17, 2020

Collection in Java provides a structure using which we can store a huge amount of data and at the same time it’s provides so many inbuilt functions or methods using which we can perform many operations such as insertion, sorting, fetching and many more.

What is the Collection in Java?

The Collection in Java is basically an interface. If we want to represent a group of individual objects as a single entity, then we go with the collection. Technically, It is a representation of a group of individual objects as a single unit.

What is a framework in Java?

A framework in Java or software framework is a basic structure that contains generic functionalities that can be extended and used or also modify the users.

Important points of the framework

  • A framework contains many inbuilt interfaces, classes, and readymade methods.
  • It is universal and reusable.
  • A framework uses is considered to be a best practice and it contains many design patterns. It is tested and proven.
  • A framework can be used to develop software, applications, and products.

Many software frameworks out there in the market such as Struts, Hibernate, Spring, Angular, and many more.

What is the Collection framework?

A collection framework is a framework that mainly deals with a group of data or collection of data. Many classes and interfaces are defined in this framework, whose main purpose is to make it easy to handle much data.

Note: The classes and interfaces of collection frameworks are present in java.util package.

Why we need a Collection framework?

To represents a collection of individual objects as a single entity, we need many classes and interfaces. And these classes and interfaces are defined in the collection framework.

Hierarchy of Collection Framework

The collection framework has two hierarchy.

  1. Collection Hierarchy.
  2. Map Hierarchy.

Collection Hierarchy

It contains the Collection Interface, which is extended by many classes. As shown in the figure.

But I have one serious question, We have already Array in Java to represent the many data as a single unit or single variable, then Why we need Collections?

To understand it in a better way, Let us look at the advantages of Collection over Array.

Difference between Array and Collection in Java

ArrayCollection

The array is fixed in size.

Explanation: The size of the array will always be the same as you assigned at the time of declaration or creating an array.

The collection is dynamic.

Explanation: The collection is growable in nature, that means even you can modify the size of the collection even after declaration as per your requirements.

 

An array is homogenous.

Explanation: It means it will store the elements of the same datatype.

 

It is heterogeneous in nature.

Explanation: It can store multiple datatypes elements together.

 

Array does not have any inbuilt method to handle the data.

Collections have many inbuild method which used to handle the data.

Example:

An array can store both primitive as well as non-primitive data.It can only store the non-primitive data. Such as String and Objects.
We cannot use any generic with the array.In Collection, we can use generics.
Performance-wise Array is a better choice.It is not recommended from the Performance aspect.
Memory point of view it is not recommended.It is suitable to use if your priorities are to save some memory space.

Methods of Collection interface

methoddescriptionReturn Type
 add(Object o)It is used to add a new element in the collection.boolean
addAll(Collection c)Used to add a group of elements or another collection’s element an existing Collection.boolean
clear()It is used to remove the elements from the collection.void
contains(Object o)To check particular elements are present in an existing Collection.boolean
containsAll(Collection c)To check a group of elements (Collection c) from the different collections are present or not.boolean
isEmpty()It is used to verify whether the collection has some element or not.boolean
iterator()In order to get a single element at a time from the collection.Iterator
remove(Object o)It is used to remove the particular Object from the collection.boolean
removeAll(Collection c)In order to remove the group of elements (Collection c) from Collection.boolean
size()It is used to count the number of elements or Objects are present in Collection.int
toArray()Used to convert the collection into Array.Object[]
retainAll(Collection c)It is used to remove all objects except the Collection c Object.boolean

Note: Iterable is the root interface in Collection Hierarchy.

Iterator interface

It is an inbuilt interface in the collection framework, which provides flexibility to iterating the element or Object in Collection.

There are three inbuild methods are in the Iterator interface.

dataTypeMethodDescription
 booleanhasNext()It checks the Collection has more elements, if there is more element then it returns true otherwise false.
Objectnext()It is used to return the next element if it is present in Collection.
voidremove()It is less popular or hardly comes into use. It removes the last element.

Iterable Interface

It is the root or parent of all the interfaces or classes present in Collection Hierarchy.

The immediate child or subinterface of the Iterable interface is Collection Interface. And after that many interfaces and class extends the Collection Interface.

Following are the Interface which extends the Collection Interface.

  1. List.
  2. Set.
  3. Queue.

Collection Interface

It is the super most interface for List, Set, and Queue Interface. It contains so many inbuild methods such as add(), addAll(), and many more which inherited to their child interface.

And all child interfaces have this method which is used to perform different operations with collection elements. Basically it provides a structure for all child interfaces and classes.

Thanks a lot for reading my article. It’s highly appreciated. I hope you must learn something new from here.

Please refer to the next article on details explanation of List, Set, and Queue Interfaces along with their implementation classes like ArrayList, LinkedList, etc.

And if you any suggestions regarding my articles Please let me know.  If you want to learn some other concept of Java, please check out the articles list.

 

Leave a Reply

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