Course contentsShow
← Java
Lesson 1048 of 2,87025. Set ImplementationsFree lesson

The Dummy Value Constant: PRESENT

How HashSet uses a single static Object instance as the value for all HashMap entries.

The Dummy Value Constant: PRESENT

You'll learn why HashSet uses a fake placeholder object for every element it stores.

The Problem HashSet Faces

You already know that HashSet is internally just a wrapper around HashMap. But there's a quirky detail: HashMap stores key-value pairs, while HashSet only needs to track keys (the elements you add). The values in the underlying HashMap serve no purpose for HashSet's functionality.

The PRESENT Constant Solution

Rather than use null or create new objects for every element, HashSet uses a single, static, dummy object called PRESENT:

// Inside HashSet's source code
private static final Object PRESENT = new Object();

When you add an element to a HashSet, it actually does this behind the scenes:

// When you call: mySet.add("apple");
// HashSet internally does:
map.put("apple", PRESENT);

Every element in your HashSet points to the exact same PRESENT object. Since HashSet never looks at the value side of the map — it only cares whether a key exists — this dummy value perfectly satisfies HashMap's requirement that every key have a value.

Why Use a Constant?

Using one shared PRESENT instance across all entries is extremely memory-efficient. Instead of creating millions of useless objects (one per element), every single entry references the same dummy object.

Key Takeaway: HashSet uses a single static PRESENT object as the value for all entries in its underlying HashMap, providing a memory-efficient placeholder that satisfies HashMap's key-value contract while being completely ignored by HashSet's logic.