Java Collections for Beginners

Last updated Apr 05, 2022


Java collection is simply a unit of objects that contains many classes and interfaces. Using a Java collection, you can manipulate and store multiple objects simultaneously. Java Collection allows you to manipulate data in many ways, including sorting, deleting, searching, and inserting. In this post, we will go through the java collection framework, the collection framework interfaces, and the classes used to implement these interfaces. 

Collection Framework

A framework is simply a set of interfaces and their implementations, such as classes, which describe a built-in architecture. The Java framework is built-in, which implies that you do not have to define it when you add new classes or features.

 

The Hierarchy of a Collection Framework

This image shows the Java Collections Hierarchy of the Collection Framework

Java Collection Types

 

The SubInterfaces of a Collection Interface

The Java collection interface is broken down into three subinterfaces implemented by different Java Classes. Here are the three subinterfaces:

  • List interface 

  • The Set interfaces

  • Queue Interface.

 

Java List Interface

The List interface extends the collection interface. As its name implies, it is a collection that contains elements sequentially, much like an array. As such, it lets you access and stores elements sequentially.

This interface is implemented using the following classes:

  • ArrayList

  • LinkedList

  • Vector

  • Stack

Java List interface types

 

ArrayList

An ArrayList class is used in the Java Collection framework to create resizable arrays.

When working with arrays in Java, you will need to define their size before utilizing them. However, it is quite difficult to change the size of an array once you define it within Java.

 To solve this problem, you should use the ArrayList class. This class enables you to construct resizable arrays.

How to create Arraylist in Java

 

LinkedList

The Java collection framework implements the linked list (doubly linked list) data structure with a LinkedList class.

There are three fields in a linked list - Prev, Next and Data. prev stores the addresses of the previous elements, next stores  the next elements in the list, and lastly, Data which contains the actual data. Determining the address of the previous element in the list can be done with the Prev field, Next field and Data field

How to create LinkedList in java

 

Vector

Just like an ArrayList, the Vector class is also used to  implement a List interface and creates resizable arrays.  It is used as an alternative to an ArrayList.

Despite the similarities in functionality between these two Java classes, the ArrayList is more preferred  to the Vectors class since the Vector is considered to be less efficient.

How to create Vector in Java

 

Stack

The stack class is used  to provide stack data structure functionality in Java.  The elements in the stack are accessed and stored based on the Last In, First Out principle, which implies that the element that was added last is the first to be accessed

How to create Stack in Java

 

Java Set Interfaces

The Set interface extends the Collection interface to provide the features of a mathematical set in Java.

In contrast to the list interface, the set interface contains duplicate items.

Since Set is a Java interface, we cannot create objects from it. To use its features, we need to use the following classes:

  • HashSet

  • LinkedHashSet

  • EnumSet

  • TreeSet

 

Java set interface tutorial

 

HashSet

This class implements the set interface.

The HashSet class is used to  implement the functionality of the hash table data structure.

How to create HashSet in Java

 

LinkedHashSet

As its name implies, the “LinkedHashSet” is a Java collection framework class that provides the functionalities of both the Linked list and the HashTable data structures.

Hash tables ensure that elements of LinkedHashSet are stored within the hash tables in the same way as HashSet, while the LinkedList specifies the order in which the elements in the hash tables will be inserted

How to Create LinkedHashSet in Java

 

EnumSet

This class also implements the Set interface. It provides a way to implement a set of elements of an enum

How to create EnumSet in Java

 

TreeSet

This class implements the functionality of a tree data structure within the Java collections framework

How to create TreeSet in Java

 

Java Queue Interface

In the Java Collections framework, the Queue interface enables the Queue data structure functionality.

This interface is implemented using the following classes:

  • ArrayDeque class

  • LinkedList class

  • PriorityQueue

 

ArrayDeque class

The ArrayDeque class is used for implementing the deque and queue data structure using arrays

This class implements the Java Queue interface and the Java Deque interface.

ArrayDeque Methods

Following are a few methods you can use to implement various functionalities in our ArrayDeque java programs:

  • add()- It will  insert the specified element into an ArrayDeque

  • addFirst()- It will  insert a specific element of an ArrayDeque at the beginning of an ArrayDeque.

  • addlast()-It will  insert a specified  element at the  end of an ArrayDeque

  • getFirst()-It will  return the first element of an ArrayDeque

  • getLast()-It will  return the last element of an ArrayDeque

  • peek()- returns the value of the first element of an ArrayDeque

  • peekFirst()- this returns  the first element in the arrayDeque  that is equivalent to the peek()

  • peekLast()- this  returns  the last element of the array deque

  • remove()- It will  return  and remove the first element from  ArrayDeque

  • remove(element)- removes the specified element

  • removeFirst()- returns and removes  the first  element from arrayDeque

removeLast()- removes  the last element in the ArrayDeque

How to create ArrayDeque in Java

 

LinkedList class

LinkedList is a class that provides the functionality of linked lists in the Java Collections Framework.

Three nodes are usually present in a linked list: the prev, next, and data. The prev node contains the address of the previous element, the next node contains the address of the next element, and the data node contains the address of the fourth element.

 

PriorityQueue

PriorityQueue provides the functionality of the heap data structure. However, unlike other queues, priorityQueues are usually retrieved in ascending order. The results are sorted. Therefore, the head of the priority queue will typically be the smallest element. After the smallest element is retrieved, the next smallest element will be retrieved to become the head

How to create PriorityQueue in Java

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

69 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions