Skip to main content
Module 6 · Lesson 1 of 213/22 in the course~16 min
Module lessons (1/2)

Set, Map and Key-Value Idioms

Lesson preview5 hands-on exercises readyCourse Java · Module 6 — Collections Framework and Generics

Uniqueness with HashSet/LinkedHashSet, dictionaries with HashMap, .getOrDefault, .computeIfAbsent and .merge.

What you'll learn in this lesson

  • Choose HashSet (or LinkedHashSet sorted by insertion) when you need duplicate-free sets in O(1) average time.
  • Handle key-value dictionaries using HashMap<K, V> and iterate them efficiently with .entrySet().
  • Eliminate boilerplate containsKey/null by adopting .getOrDefault(), .computeIfAbsent() and .merge().

Overview

When searching within a list becomes too slow (O(N)), Set and Map offer instant searches (O(1) medium via hashing).

A Set<E> does not allow duplicates: .add(x) returns false if x is already present according to .equals() and .hashCode().

Sections covered in this lesson

  1. 11. HashSet, LinkedHashSet and TreeSet: Guaranteed Uniqueness
  2. 22. HashMap<K, V>: Key-Value Associations
  3. 33. Counters and Idiomatic Groupings (merge / computeIfAbsent)
  4. 44. Iterate Clean with entrySet()
  5. 5Try it yourself
Pro Lesson · Module 6 — Collections Framework and Generics

Unlock "Set, Map and Key-Value Idioms" and the full Course Java curriculum

Uniqueness with `HashSet`/`LinkedHashSet`, dictionaries with `HashMap`, `.getOrDefault`, `.computeIfAbsent` and `.merge`.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course Java plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.