Collections in JAVA

This post deals with collections in java

5/1/20232 min read

Collection is a framework in java,it was introduced in Java 1.2 .This framework provides interfaces and classes which assist in storing objects and manipulating them. It provides a correlation between different classes and interface and links them with each other.Before collection framework hashtable ,arrays were used for grouping objects but they didn't have any link between them.

Iterable interface

Iterable is the root interface of the collection framework and all the other classes and interfaces implement or extend it,hence the iterable method present in the collection framework is available for all the other classes ,it is used to iterate over the objects of the collection.

Iterable interface has three methods next() ,hasNext() and remove(). next() ,hasNext() can be used to iterate over the elements of the collection.

Other interfaces of collection framework

Collection Interface

Collection interface extends the iterable interface ,all the classes of the collection framework implement the collection interface.It contains basic methods which can be used to manipulate the objects of the collection.Some methods present in collection interface are add(E e),addAll(Collection<? extends E> d),remove(Object e)

List Interface

It extends the collection interface.It stores the elements of the collection in an ordered manner,duplicate values can be entered in collections of list type.Some of the classes extending the list interface are ArrayList, LinkedList, Vector.

Queue Interface

This interface is used when we try to retrieve and store the data in a specific order as the name suggests that specific order in FIFO(First in first out).

This means that the element which was inserted first in the collection will be the first to be accessed ,for example if A,B,C are inserted in the collection in that order, then when we will access the elements we will first get element A ,then B, then C.

DEQUE Interface

In this type of collection the objects can be inserted from both the ends of the collection,unlike queue interface where the objects can be inserted only from one end in the collection.This interface is a child of queue interface.

Set Interface

This collection doesn't maintain the order of the elements and also doesn't allow duplicates.The classes implementing the set interface are HashSet ,LinkedHashSet,TreeSet.