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 (2)

Collapse
 
linjunjie525 profile image
Lin JunJie

Excellent analysis!
It's very clear and includes actual code, making it perfect for beginners who want to get started with Java collections.
I especially like the explanation of "when to use it." It's very practical!

Collapse
 
mohamad_mhana profile image
Mohammed mhanna

Thanks so much! Iโ€™m glad it was helpful.