Java Data Structures
Data structures are fundamental concepts in programming that define how data is stored, organized, and managed efficiently. Choosing the right data structure improves the performance, scalability, and readability of a program.
In Java, many powerful data structures are available in the java.util package. These structures provide advanced functionality beyond traditional arrays and help developers manipulate collections of data effectively.
Some of the most widely used Java data structures include:
- ArrayList
- HashSet
- HashMap
Think of these as advanced versions of arrays—they provide more flexibility, automatic resizing, and useful built-in methods.
In this guide, we will briefly introduce each structure and understand how they work.
ArrayList in Java
An ArrayList is a dynamic array that can automatically grow or shrink in size as elements are added or removed. Unlike normal arrays, you do not need to specify the size in advance.
Key Features
- Maintains the insertion order
- Allows duplicate elements
- Supports index-based access
- Automatically resizes
Example Program: Managing Student Names
import java.util.ArrayList;
public class StudentListDemo {
public static void main(String[] args) {
// Creating an ArrayList to store student names
ArrayList<String> students = new ArrayList<>();
// Adding elements
students.add("Aisha");
students.add("Rahul");
students.add("Meera");
students.add("David");
// Accessing element by index
System.out.println("First Student: " + students.get(0));
// Displaying all students
System.out.println("Student List:");
for(String name : students) {
System.out.println(name);
}
}
}
When to Use ArrayList
Use ArrayList when:
- You need fast access using an index
- The number of elements changes frequently
- Duplicate values are acceptable
HashSet in Java
A HashSet is a collection that stores unique elements only. It automatically removes duplicates and does not guarantee insertion order.
Key Features
- No duplicate elements
- Unordered collection
- Provides fast lookup operations
Example Program: Tracking Unique Email IDs
import java.util.HashSet;
public class UniqueEmailDemo {
public static void main(String[] args) {
HashSet<String> emails = new HashSet<>();
emails.add("user1@gmail.com");
emails.add("user2@gmail.com");
emails.add("admin@gmail.com");
emails.add("user1@gmail.com"); // Duplicate
System.out.println("Registered Email IDs:");
for(String email : emails) {
System.out.println(email);
}
}
}
Important Note
Even though "user1@gmail.com" is inserted twice, it will only appear once because HashSet automatically removes duplicates.
When to Use HashSet
Use HashSet when:
- You need unique elements
- Order does not matter
- You want fast duplicate detection
HashMap in Java
A HashMap stores data as key-value pairs. Each key is unique and maps to a specific value.
It is widely used when data needs to be retrieved quickly using a key.
Key Features
- Stores key-value pairs
- Keys must be unique
- Values can be duplicated
- Provides fast lookup performance
Example Program: Student Marks System
import java.util.HashMap;
public class StudentMarksDemo {
public static void main(String[] args) {
HashMap<Integer, String> studentRecords = new HashMap<>();
// Adding student ID and name
studentRecords.put(101, "Kiran");
studentRecords.put(102, "Sneha");
studentRecords.put(103, "Arjun");
studentRecords.put(104, "Priya");
// Retrieve a value using key
System.out.println("Student with ID 102: " + studentRecords.get(102));
// Display all records
System.out.println("\nAll Student Records:");
for(Integer id : studentRecords.keySet()) {
System.out.println("ID: " + id + " Name: " + studentRecords.get(id));
}
}
}
When to Use HashMap
Use HashMap when:
- Data must be accessed quickly using a key
- You are storing paired information (ID → Name, Product → Price, etc.)
Iterators in Java
When working with Java collections, you will often encounter Iterators.
An Iterator is an object used to traverse (loop through) elements in a collection safely.
It provides methods such as:
- hasNext() → checks if another element exists
- next() → retrieves the next element
Example Program: Using an Iterator
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorDemo {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
Iterator<Integer> iterator = numbers.iterator();
System.out.println("Traversing List Using Iterator:");
while(iterator.hasNext()) {
int value = iterator.next();
System.out.println(value);
}
}
}
Why Use Iterators?
Iterators are useful because they:
- Provide a standard way to traverse collections
- Work with all Java collection classes
- Allow safe removal of elements during iteration