DEV Community

Mohammed mhanna
Mohammed mhanna

Posted on

🧺 Built-in Data Structures in Java

When you start writing real-world Java programs, managing data efficiently becomes crucial. That’s where Java’s built-in data structures come into play — they help you store, organize, and manipulate data with ease.

Let’s explore the most important ones, when to use each, and how they make your programs more powerful 💪


🧠 What Are Data Structures?

A data structure is a way of organizing and storing data so it can be accessed and modified efficiently.

In Java, the Collections Framework and arrays provide ready-to-use data structures like lists, sets, and maps — all part of java.util.


🧩 1. Arrays

✅ Fixed size and fast access

int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[2]); // Output: 3
Enter fullscreen mode Exit fullscreen mode

When to use:
When the number of elements is known in advance, and you need fast random access.

Limitation:
Size cannot be changed once created.

📘 More: Java Arrays (Official Docs)


📜 2. ArrayList

✅ Dynamic arrays with resizing
✅ Maintains insertion order

import java.util.ArrayList;

ArrayList<String> names = new ArrayList<>();
names.add("Ali");
names.add("Omar");
names.add("Sara");
System.out.println(names); // [Ali, Omar, Sara]
Enter fullscreen mode Exit fullscreen mode

When to use:
When you need a resizable list that can grow or shrink dynamically.

📘 More: ArrayList Docs


🔁 3. LinkedList

✅ Doubly linked structure
✅ Efficient for frequent insertions and deletions

import java.util.LinkedList;

LinkedList<Integer> list = new LinkedList<>();
list.add(10);
list.add(20);
list.addFirst(5);
System.out.println(list); // [5, 10, 20]
Enter fullscreen mode Exit fullscreen mode

When to use:
When you frequently insert or remove elements, especially in the middle of the list.

📘 More: LinkedList Docs


🚦 4. HashSet

✅ Unique elements only
✅ Fast lookups (based on hashing)

import java.util.HashSet;

HashSet<String> set = new HashSet<>();
set.add("Java");
set.add("Python");
set.add("Java"); // duplicate ignored
System.out.println(set); // [Java, Python]
Enter fullscreen mode Exit fullscreen mode

When to use:
When you need to store unique items and don’t care about order.

📘 More: HashSet Docs


🗺 5. HashMap

✅ Stores key-value pairs
✅ Fast retrieval by key

import java.util.HashMap;

HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Ali");
map.put(2, "Omar");
System.out.println(map.get(1)); // Ali
Enter fullscreen mode Exit fullscreen mode

When to use:
When you need to associate keys with values — like a dictionary or lookup table.

📘 More: HashMap Docs


⏫ 6. Stack

✅ Last In, First Out (LIFO) structure

import java.util.Stack;

Stack<String> stack = new Stack<>();
stack.push("First");
stack.push("Second");
System.out.println(stack.pop()); // Second
Enter fullscreen mode Exit fullscreen mode

When to use:
When you need to process elements in reverse order (e.g., undo operations, expression parsing).

📘 More: Stack Docs


⏬ 7. Queue

✅ First In, First Out (FIFO) structure

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

Queue<String> queue = new LinkedList<>();
queue.add("Task1");
queue.add("Task2");
System.out.println(queue.remove()); // Task1

Enter fullscreen mode Exit fullscreen mode

When to use:
When you need to process elements in the same order they were added (e.g., scheduling tasks, print jobs).

📘 More: Queue Docs


🧭 Choosing the Right Data Structure

Goal Use This
Fixed number of items Array
Dynamic list of elements ArrayList
Frequent insertions/removals LinkedList
Unique elements HashSet
Key-value pairs HashMap
LIFO (Reverse order) Stack
FIFO (Normal order) Queue

🧠 Final Thoughts

Java’s built-in data structures are designed to make your life easier — you just need to know which one fits the job.

Start small:

Understand how each structure stores and accesses data.

Practice choosing the right one for the right scenario.

Then explore advanced ones like TreeSet, PriorityQueue, and `ConcurrentHashMap.

`

❓Question for You

Which built-in data structure do you use most in your Java projects — and why?
Share your answer in the comments 👇

Top comments (0)