Home Core Java Overview Maven Tutorials

Friday, 11 January 2019

Collection in Java

A brief intro of Java Collections

Hello Friends,
In this article , i will describe about collection in Java Language :-

Java collections refer to a single unit of objects.
You can perform all operations on data such as searching, sorting, insertion, manipulation, deletion, etc. by Java collections.
The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects.
Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).
What is a Java Collection Framework?
A Java collection framework provides an architecture to store and manipulate a group of objects. A Java collection framework includes the following:
Let’s learn about them in detail:
Interfaces: Interface in Java refers to the abstract data types. They allow Java collections to be manipulated independently from the details of their representation. Also, they form a hierarchy in object-oriented programming languages.
Classes: Classes in Java are the implementation of the collection interface. It basically refers to the data structures that are used again and again.
Algorithm: Algorithm refers to the methods which are used to perform operations such as searching and sorting, on objects that implement collection interfaces. Algorithms are polymorphic in nature as the same method can be used to take many forms or you can say perform different implementations of the Java collection interface.
The Java collection framework provides the developers to access prepackaged data structures as well as algorithms to manipulate data. Next, let us move to the Java collections framework hierarchy and see where these interfaces and classes resides.

Java Collections : Interface
Iterator interface : Iterator is an interface that iterates the elements. It is used to traverse the list and modify the elements. Iterator interface has three methods which are mentioned below:
public boolean hasNext() – This method returns true if the iterator has more elements.
public object next() – It returns the element and moves the cursor pointer to the next element.
public void remove() – This method removes the last elements returned by the iterator.

There are three components that extend the collection interface i.e List, Queue and Sets. Let’s learn about them in detail:
Java collections: List A List is an ordered Collection of elements which may contain duplicates. It is an interface that extends the Collection interface. Lists are further classified into the following:
Let’s go into detail on each one of them:
Array list: ArrayList is the implementation of List Interface where the elements can be dynamically added or removed from the list. Also, the size of the list is increased dynamically if the elements are added more than the initial size.
ArrayList object = new ArrayList ();

Linked List: Linked List is a sequence of links which contains items. Each link contains a connection to another link.
Syntax: Linkedlist object = new Linkedlist();

Java Linked List class uses two types of Linked list to store the elements:
Singly Linked List
Doubly Linked List
Singly Linked List: In a singly Linked list each node in this list stores the data of the node and a pointer or reference to the next node in the list. Refer to the below image to get a better understanding of single Linked list.

Doubly Linked List: In a doubly Linked list, it has two references, one to the next node and another to previous node. You can refer to the below image to get a better understanding of doubly linked list.

No comments:

Post a Comment