Java Data Structures


Data structures are the backbone of every program. They help organize and store data efficiently so that it can be accessed, updated, and managed easily. In Java, data structures are implemented through classes and interfaces in the Java Collections Framework (JCF).

Whether you’re storing a list of student names, mapping employee IDs to salaries, or sorting customer records, choosing the right data structure can significantly improve your program’s performance.

What Are Data Structures?

A data structure is a way of organizing data in memory so that it can be used effectively. In simple terms, it defines how data is arranged, accessed, and modified.
Java supports many data structures such as arrays, lists, sets, and maps. Each has a specific purpose. For example, lists maintain order, sets store unique values, and maps store key–value pairs.

Why Are Data Structures Important?

Using proper data structures improves the efficiency of your code. Without them, your program might run slower, take more memory, or be difficult to maintain.
Key advantages include:

  • Faster searching and sorting

  • Easier data manipulation

  • Better memory management

  • Cleaner, organized code

For example, if you need to store names where duplicates aren’t allowed, you should use a HashSet. If you want to maintain an ordered collection, you can use an ArrayList.

Built-in Data Structures in Java

Java provides two main types of data structures:

  1. Primitive Data Structures – These include basic types like int, float, char, and boolean. They hold single values.

  2. Non-Primitive Data Structures – These include complex structures such as arrays, classes, and objects. Most modern Java applications use non-primitive structures from the Collections Framework.

Java Collections Framework (JCF)

The Java Collections Framework is a powerful API that provides ready-to-use data structures and algorithms. It is part of java.util package and includes interfaces and classes for lists, sets, queues, and maps.

Main Interfaces in JCF:

  • Collection – The root interface for most collections.

  • List – Stores ordered elements (e.g., ArrayList, LinkedList).

  • Set – Stores unique elements (e.g., HashSet, LinkedHashSet).

  • Queue – Follows FIFO (First In, First Out) order (e.g., PriorityQueue).

  • Map – Stores key–value pairs (e.g., HashMap, TreeMap).

Arrays in Java

An array is the simplest data structure in Java. It stores elements of the same type in a fixed-size sequence.

Example:

public class ArrayExample {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40, 50};
        for (int num : numbers) {
            System.out.println(num);
        }
    }
}

Arrays are useful for small collections where the number of elements doesn’t change.

Lists in Java

A List is a dynamic data structure that can grow or shrink as needed. Unlike arrays, lists can store different object references and adjust their size automatically.

Example using ArrayList:

import java.util.ArrayList;

public class ListExample {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("Ananya");
        names.add("Kavya");
        names.add("Diya");
        System.out.println(names);
    }
}

Lists maintain insertion order and allow duplicate elements.

Sets in Java

A Set is used to store unique elements. It does not allow duplicates and does not guarantee order.

Example using HashSet:

import java.util.HashSet;

public class SetExample {
    public static void main(String[] args) {
        HashSet<String> cities = new HashSet<>();
        cities.add("Delhi");
        cities.add("Mumbai");
        cities.add("Delhi");
        System.out.println(cities);
    }
}

Even though “Delhi” was added twice, it will appear only once in the output.

Maps in Java

A Map stores data in key–value pairs. Keys are unique, but values can be duplicated.

Example using HashMap:

import java.util.HashMap;

public class MapExample {
    public static void main(String[] args) {
        HashMap<Integer, String> students = new HashMap<>();
        students.put(101, "Ananya");
        students.put(102, "Kavya");
        students.put(103, "Diya");
        System.out.println(students);
    }
}

Maps are perfect for scenarios where each element has a unique identifier.

Queues in Java

A Queue stores elements in a sequence where the first element added is the first one removed (FIFO order).

Example:

import java.util.LinkedList;
import java.util.Queue;

public class QueueExample {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();
        queue.add("Task 1");
        queue.add("Task 2");
        queue.add("Task 3");
        System.out.println(queue.remove()); // removes Task 1
    }
}

Choosing the Right Data Structure

Here’s a quick guide to choosing the best data structure:

Need Recommended Data Structure
Fixed number of elements Array
Dynamic list of elements ArrayList or LinkedList
Unique items HashSet
Key–value pairs HashMap
FIFO order Queue
Sorted data TreeSet or TreeMap

Selecting the right one helps make your code efficient and easier to maintain.

Summary of the Tutorial

In this tutorial, you learned about data structures in Java and how they help organize and manage data efficiently. You explored arrays, lists, sets, queues, and maps, which form the core of the Java Collections Framework. Understanding these structures is crucial for writing optimized, clean, and scalable Java applications.


Practice Questions

  1. Create an array to store the names of five students and display them using a loop.

  2. Write a Java program to find the largest element in an integer array.

  3. Create a program that stores marks of 10 students in an array and calculates the average marks.

  4. Write a program to reverse an array without using a second array.

  5. Implement a program that checks whether a given element exists in an array or not.

  6. Write a Java program to count the number of even and odd numbers in an array.

  7. Create a 2D array representing a 3x3 matrix and display its diagonal elements.

  8. Write a Java program to copy all elements from one array to another.

  9. Implement a program that finds the second largest number in an array.

  10. Create a Java program that merges two arrays into one and displays the combined array.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top