Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Java Core Fundamentals

Object-Oriented Programming

  • Classes and Objects
  • Inheritance and Polymorphism
  • Interfaces and Abstract Classes

Collections Framework

  • List, Set, and Map
  • Generics

Exceptions & Best Practices

  • Exception Handling
  • equals() and hashCode()
  • String Manipulation
Chaturmind
← Java Core Fundamentals

Object-Oriented Programming

  • Classes and Objects
  • Inheritance and Polymorphism
  • Interfaces and Abstract Classes

Collections Framework

  • List, Set, and Map
  • Generics

Exceptions & Best Practices

  • Exception Handling
  • equals() and hashCode()
  • String Manipulation
HomeLearnJavaJava Core FundamentalsCollections Framework
✓ FreeBeginner· 12 min read

List, Set, and Map

Pick the right collection for every use case.

Published January 18, 2025


Java Collections: List, Set, and Map

The Java Collections Framework provides data structures for almost every need.

List — ordered, allows duplicates

List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Alice"); // duplicates allowed
System.out.println(names.get(0)); // Alice

Set — unique elements, no duplicates

Set<String> tags = new HashSet<>();
tags.add("java");
tags.add("java"); // second add is ignored
System.out.println(tags.size()); // 1

Map — key-value pairs

Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 95);
scores.put("Bob", 87);
System.out.println(scores.get("Alice")); // 95
scores.getOrDefault("Charlie", 0);       // 0 — no NPE

Choosing the right collection

NeedUseWhy
Ordered list, allow duplicatesArrayListO(1) random access
Fast insert/delete at endsLinkedList (as Deque)O(1) at both ends
No duplicates, fast lookupHashSetO(1) average
No duplicates, sortedTreeSetO(log n), sorted order
Key-value lookupHashMapO(1) average
Key-value, sorted keysTreeMapO(log n), sorted
Key-value, insertion orderLinkedHashMapO(1), predictable order

Interview: equals and hashCode

For HashSet and HashMap to work correctly, your objects must implement both equals() and hashCode() consistently.

// Two objects that are equal MUST have the same hashCode
// Objects with the same hashCode may or may not be equal (hash collision)

Interview Tip

Always know the complexity of the operations you're using. ArrayList.add(index, x) is O(n) because it shifts elements — a common gotcha.

Previous

Interfaces and Abstract Classes

Next

Generics

AI Tutor

Lesson: List, Set, and Map

Quick actions

AI responses can be inaccurate. Verify critical information.