Data Structures in Java - Types, Operations & Examples

Updated on 03/09/20255,276 Views

Data structures in Java play a critical role in organizing and managing data efficiently. They determine how data is stored, accessed, and processed in applications. Choosing the right data structure directly impacts performance and functionality. In everyday scenarios like searching products in an app or handling requests on a server, data structures make operations faster and more reliable. 

This tutorial on data structures in Java explains the basics and advanced concepts with clarity. You will learn about primitive and non-primitive data structures, their types, operations, and real-world use cases. With examples in Java, the blog provides a structured approach to understand arrays, linked lists, stacks, queues, trees, graphs, and more. 

Looking to advance your Java skills? Check out upGrad’s Online Software Engineering Courses. Start today to strengthen your foundation and accelerate your career growth by learning JavaScript, Node.js, APIs, React, and more! 

What are Data Structures in Java? 

Data structures in Java are specialized formats used to store, organize, and manage data efficiently. They provide a way to access, modify, and process data for different applications. Java supports both primitive and non-primitive data structures, enabling developers to handle simple as well as complex datasets. Common data structures in Java include arrays, linked lists, stacks, queues, trees, heaps, and graphs, each serving unique use cases in application development. 

Accelerate your tech career by mastering future-ready skills in Cloud, DevOps, AI, and Full Stack Development. Gain hands-on experience, learn from industry leaders, and develop the expertise that top employers demand. 

  • Professional Certificate Program in Cloud Computing and DevOps
  • AI-Powered Full Stack Development Course by IIITB
  • Future-Proof Your Tech Career with AI-Driven Full-Stack Development

Need for Data Structures in Java

Applications get complicated due to the large amount of data. This leads to various problems in handling the data.

1. As the data grows rapidly high processing speed is required to handle a large amount of data.

2. Searching for the required data from a massive amount of data makes the searching process slow down.

3. Multiple requests by millions of users at a single time may lead to server breakdown.

To overcome these problems data structure is used. This technique helps the user to get the required data instantly.

Types of Data Structures

Mainly there are two types of data structure

1. primitive data structure

2. Non-primitive data structure

Some common types of data structures include

1. Array

2. Linked List

3. Stack

4. Queue

5. Graph

6. set

Primitive Data Structure

Primitive data structures are primitive data types that include int, char, float, double, and pointer. These data structures hold a single value.

Non-primitive Data Structure

Non-primitive data structures can be classified into two types:

1. Linear data structure: In linear data structure the data are arranged in a sequential manner in which one element is connected to another element in linear form. These are single-level data structures. Arrays, linked lists, stacks, and queues are known as linear data structures.

2. Nonlinear data structure: In nonlinear data structure, one element is connected to the ‘n’ number of elements. In this structure, elements are arranged in random ways. These are multi-level data structures. Trees and graphs are known as nonlinear data structures.

Major Operations

The following operations can be performed on a data structure.

1. Searching: An element in a data structure can be easily searched.

2. Sorting: Elements of a data structure can be sorted either in ascending or descending order.

3. Insertion: A new element can be inserted in a data structure.

4. Updation: An element in the data structure can be replaced by another element.

5. Deletion: An element can be deleted from the data structure by using this operation.

Must Read: Difference Between Linear and Non-Linear Data Structures

Which Data Structure Should You Select?

A particular ADT (Abstract Data type) is executed by using some data structure. In this process, ADT gives an idea about what is to be done, and the data structure tells how it is to be implemented. In a particular ADT, various types of data structures can be performed depending on the time and space. So, which data structure is to be selected depends upon the user’s requirements.

Advantages of Data Structures

1. Efficient organization and storage of data: The data on data structure can be easily accessed, retrieved, and modified. Through efficient organization techniques, data structure makes it easy to handle complex data relationships.

2. Developed time and space complexity: This feature helps in performing major operations such as searching, insertion, deletion, and sorting in an efficient way. The user can select the appropriate data structure according to the priority based on space and time.

3. Improved data manipulation: Complex data manipulation, such as sorting, merging, and searching, is executed by using the data structure.

4. Flexibility and adaptability: With the help of object-oriented programming principles, custom data structures can be created.

Data Structures Index 

Basics of Data Structures in Java 

  • DS Tutorial – Step-by-step guide to learning data structures in Java. 
  • Introduction to DS – Overview of concepts and applications. 
  • Algorithms – Understanding algorithmic techniques with data structures. 
  • Asymptotic Analysis – Measuring efficiency of algorithms. 
  • Pointers and Structures – Low-level handling of memory and data representation. 

Arrays 

  • 1D Array – Stores elements of the same type in contiguous memory. 
  • 2D Array – Matrix-like structure with rows and columns. 

Linked Lists 

  • Singly Linked List – Linear list with nodes connected one-way. 
  • Insertion (beginning, end, after node) 
  • Deletion (beginning, end, after node) 
  • Traversing and Searching 
  • Doubly Linked List – Nodes with references to both next and previous. 
  • Insertion (beginning, end, after node) 
  • Deletion (beginning, end, by data) 
  • Traversing and Searching 
  • Circular Linked List – Nodes connected in a circular manner, no null at the end. 
  • Insertion (beginning, end) 
  • Deletion (beginning, end) 
  • Traversing and Searching 
  • Circular Doubly Linked List – Combines circular and doubly linked list features. 

Stack 

  • Stack in Java – LIFO structure for storing and managing data. 
  • Array Implementation 
  • Linked List Implementation 

Queue 

  • Queue in Java – FIFO structure for sequential processing. 
  • Array Implementation 
  • Linked List Implementation 
  • Circular Queue 

Trees 

  • Binary Tree – Hierarchical structure with two children per node. 
  • Pre-order, In-order, Post-order Traversal 
  • Searching, Insertion, Deletion 
  • Rotations (LL, LR, RL, RR) 
  • Insertion and Deletion 
  • B-Tree – Multi-way search tree for databases and file systems. 

Graphs 

  • Graph in Java – Collection of vertices and edges. 
  • Implementation 
  • BFS (Breadth First Search) 
  • DFS (Depth First Search) 
  • Spanning Tree 

Searching Algorithms 

  • Linear Search – Sequential search method. 
  • Binary Search – Efficient search for sorted data. 

Sorting Algorithms 

  • Bubble Sort 
  • Bucket Sort 
  • Comb Sort 
  • Counting Sort 
  • Heap Sort 
  • Insertion Sort 
  • Merge Sort 
  • Quick Sort 
  • Radix Sort 
  • Selection Sort 
  • Shell Sort 
  • Bitonic Sort 
  • Cocktail Sort 
  • Cycle Sort 
  • Tim Sort 

Classification of Data Structures

Singly Linked List

This linked list contains a node with a single pointer pointing to the next node. So this is also called a one-way list. This linked list stores data and references to the next node or a null value. The start pointer stores the linked list's first address and the last node's next pointer null value.

Circular Linked List

In a circular linked list, all the nodes are aligned to form a circle. Any node can be considered as a first node, and no null node at the end.

Doubly Linked List

In a doubly linked list traversing in both directions is possible. This linked list contains two pointers n which one is pointing to the next node and another is pointing to the previous node.

Example: A Java program to show the implementation of a linked list.

import java.util.*;  
public class LinkedList{  
     public static void main(String args[]){  
         
         LinkedList<String> ll=new LinkedList<String>();
         ll.add("Red");  
         ll.add("Blue");  
         ll.add("Yellow");  
         ll.add("Orange");  
         
         System.out.println(ll);
         
         }  
}  

Stack

Stack follows the last in first out (LIFO) data structure. It can be implemented as an array or linked list. Insertion in a stack is known as pushing denoted as push() and deletion is known as popping denoted as pop() and both operations can be done at the top of the stack only. Stacks can be used in parenthesis matching, solving the maze problem, nested function calls, etc. The syntax of the stack is represented below.

Stack var1 = new Stack(size);

Queue

Queue follows the first in first out (FIFO) data structure. In this insertion is done at the rear end denoted as enqueue() and deletion is done at the front end of the queue denoted as dequeue().

Binary Search Tree

Here the elements are arranged by following some order. In a binary search tree, the value of the left node must be lesser than the parent node, and the value of the right node must be larger than the parent node. Various operations such as searching, insertion, and deletion is easy in the binary search tree.

Heap

Applications related to priority, scheduling algorithms, caching, etc can be executed using a heap. Heap is a tree-based data structure in which the tree is considered as a complete binary tree. In this tree, the node can have utmost two children.

Hashing

Hashing is used to quickly rapidly a particular value within a given array. In this, each element has a unique hash code and the hash code is stored instead of the actual element.

Graph

This nonlinear data structure consists of vertices and edges. The vertices are referred to as nodes and edges are lines that again connect any two nodes in the graph. Connected data are stored by using a graph data structure. An example of graph data structure includes a network of people or a network of cities.

Array

The array is a collection of similar data items stored at connecting memory locations. In an array fixed size elements of the same data type are stored.

Example: A program to show the implementation of the array.

class Array
{
    public static void main (String[] args)
    {        
      int[] array;
               array = new int[5];
               array[0] = 1;
               array[1] = 4;     
      array[2] = 15;
      array[3] = 21;
      array[4] = 6;
      for (int i = 0; i < arr.length; i )
         System.out.println(array[i]);   
    }
}

Advantages of Arrays

1. Easily store elements of the same data type in.

2. Other data structures like stack and queue can also be implemented using an array.

3. There is no issue of overload or shortage of memory.

4. Data in arrays can be easily accessed.

Disadvantages of Arrays

1. Size of the array cannot be changed in the array.

2. In an array heterogeneous data cannot be stored.

Matrix

In a matrix, numbers are stored in rows and columns.

Conclusion

Data structures in Java are the backbone of efficient programming. They help in storing, organizing, and processing data effectively. From arrays and linked lists to trees and graphs, each structure serves a unique purpose in solving real-world problems. Choosing the right data structure improves performance and reduces complexity.  

This tutorial covered the basics, operations, and types of data structures in Java. With practice, learners can apply these concepts in applications such as databases, operating systems, and algorithms. Mastering data structures in Java is essential for building scalable and optimized software solutions.

FAQs

1. What is data structure?

The data structure can be defined as a collection of data with precise operations and qualities. Data structure helps users simply access the required data.

2. What are the major operations of data structure?

The major operations of data structure include searching, sorting, deletion, and updation.

3. What are the main types of data structure?

The different types of data structures include arrays, stacks, linked lists, queues, sets, and graphs.

image

Take the Free Quiz on Java

Answer quick questions and assess your Java knowledge

right-top-arrow

FREE COURSES

Start Learning For Free

image
Pavan Vadapalli

Author|911 articles published

Pavan Vadapalli is the Director of Engineering , bringing over 18 years of experience in software engineering, technology leadership, and startup innovation. Holding a B.Tech and an MBA from the India....

image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Explore Our Free Software Tutorials

Top Resources

Recommended Programs

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 10 AM to 7 PM

text

Indian Nationals

text

Foreign Nationals