JAVA Tutorial
Estimated reading: 4 minutes 333 views

Java Data Structures – Master Collections, Lists, and Maps (2025 Guide)


Introduction – Why Learn Java Data Structures?

Java provides a robust Collections Framework to handle, store, and manipulate dynamic groups of objects efficiently. Understanding Java data structures helps in writing scalable, efficient, and organized programs.

In this guide, you’ll learn:

  • What Java data structures are and why they matter
  • How to use ArrayList, LinkedList, and HashMap
  • How to sort lists, use iterators, and understand wrapper classes

Topics Covered

Topic Description
Java Data StructuresOverview of collection types and utilities
Java ArrayListDynamic arrays with fast access
Java LinkedListNode-based list with quick insertions/deletions
Java List SortingSorting elements using Collections.sort()
Java HashMapKey-value pair data structure
Java IteratorSequential access and safe element removal
Java Wrapper ClassesConverting primitives into objects (e.g., int β†’ Integer)

Java Data Structures – The Foundation

Java’s data structures allow you to store and organize data in different ways:

  • List β†’ Ordered collection
  • Set β†’ Unordered unique elements
  • Map β†’ Key-value pairs

We’ll focus on List and Map, two of the most commonly used.


Java ArrayList – Resizable Array

The ArrayList class provides a dynamic array with fast random access.

Example:

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> colors = new ArrayList<>();
    colors.add("Red");
    colors.add("Blue");
    colors.add("Green");

    System.out.println(colors.get(1)); // Output: Blue
  }
}

Line-by-Line Explanation:

  • ArrayList<String> β†’ Declares a dynamic list of strings.
  • .add(...) β†’ Adds items to the list.
  • .get(1) β†’ Retrieves element at index 1.

Java LinkedList – Efficient Insertions

LinkedList allows faster insertions/removals compared to ArrayList.

Example:

import java.util.LinkedList;

public class Main {
  public static void main(String[] args) {
    LinkedList<String> items = new LinkedList<>();
    items.add("Milk");
    items.addFirst("Bread");
    items.addLast("Eggs");

    System.out.println(items); // Output: [Bread, Milk, Eggs]
  }
}

Line-by-Line Explanation:

  • addFirst() β†’ Inserts at the beginning.
  • addLast() β†’ Inserts at the end.

Java List Sorting – Organize Data

Use Collections.sort() to sort a list of elements.

Example:

import java.util.*;

public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(5, 2, 8, 1));
    Collections.sort(numbers);
    System.out.println(numbers); // Output: [1, 2, 5, 8]
  }
}

Line-by-Line Explanation:

  • Arrays.asList(...) β†’ Creates list from array.
  • Collections.sort(...) β†’ Sorts the list in ascending order.

Java HashMap – Key-Value Store

Use HashMap when you want to store data with unique keys.

Example:

import java.util.HashMap;

public class Main {
  public static void main(String[] args) {
    HashMap<String, Integer> ageMap = new HashMap<>();
    ageMap.put("Alice", 28);
    ageMap.put("Bob", 35);

    System.out.println(ageMap.get("Bob")); // Output: 35
  }
}

Line-by-Line Explanation:

  • .put(key, value) β†’ Inserts key-value pair.
  • .get(key) β†’ Retrieves value using the key.

Java Iterator – Traverse Collections Safely

Iterators allow sequential access and element removal during iteration.

Example:

import java.util.*;

public class Main {
  public static void main(String[] args) {
    ArrayList<String> fruits = new ArrayList<>(List.of("Apple", "Banana", "Cherry"));
    Iterator<String> it = fruits.iterator();

    while (it.hasNext()) {
      System.out.println(it.next());
    }
  }
}

Line-by-Line Explanation:

  • .iterator() β†’ Creates an iterator object.
  • .hasNext() β†’ Checks for more elements.
  • .next() β†’ Retrieves next element.

Java Wrapper Classes – Primitives as Objects

Wrapper classes encapsulate primitive types into objects.

PrimitiveWrapper Class
intInteger
charCharacter
booleanBoolean

Example:

public class Main {
  public static void main(String[] args) {
    int num = 10;
    Integer wrappedNum = num;  // Autoboxing
    System.out.println(wrappedNum);
  }
}

Line-by-Line Explanation:

  • Integer wrappedNum = num; β†’ Converts primitive to object automatically.

Summary – Recap & Next Steps

Understanding and applying data structures is essential for real-world Java programming.

Key Takeaways:

  • ArrayList and LinkedList manage dynamic lists efficiently
  • HashMap offers quick key-based access to data
  • Iterator lets you loop and remove safely
  • Wrapper Classes let primitives act like objects
  • Collections.sort() helps organize your list data

What’s Next?

  • Explore HashSet, TreeMap, and PriorityQueue
  • Learn custom sorting with Comparator
  • Build CRUD apps using ArrayList and HashMap

Frequently Asked Questions (FAQs)


When should I use ArrayList vs LinkedList?
Use ArrayList for fast access and LinkedList for frequent insertions/deletions.


Can I store primitive data types in collections?
No. Use wrapper classes like Integer, Double, etc., since collections store objects.


Is HashMap ordered?
No. Use LinkedHashMap or TreeMap if you need insertion or sorted order.


How do I remove elements while iterating a list?
Use an iterator and its remove() method to safely remove elements.


What’s the difference between autoboxing and unboxing?
Autoboxing converts primitives to objects.
Unboxing converts objects back to primitives.


Share Now :
Share

πŸ“Š Java Data Structures

Or Copy Link

CONTENTS
Scroll to Top