ZetCode

Java Collections.emptySet Method

Last modified: April 20, 2025

The Collections.emptySet method returns an immutable empty set. It is part of Java's Collections utility class. This method provides a type-safe way to get an empty set instance.

The returned set is serializable and implements the Set interface. It is immutable, meaning any modification attempt throws an exception. This makes it thread-safe and memory efficient.

Collections.emptySet Overview

Collections.emptySet returns a singleton empty Set instance. It was introduced in Java 1.5 as part of the Collections Framework. The method is generic and returns a Set of the specified type.

The main advantage is memory efficiency. Since the set is empty and immutable, the same instance can be reused. This avoids unnecessary object creation. It's ideal for methods that need to return empty sets.

Basic emptySet Usage

This example demonstrates the most basic usage of Collections.emptySet. We obtain an empty set and verify its properties. The example shows the set's immutability by attempting to modify it.

EmptySetBasic.java
package com.zetcode;

import java.util.Collections;
import java.util.Set;

public class EmptySetBasic {

    public static void main(String[] args) {
        
        Set<String> emptySet = Collections.emptySet();
        
        System.out.println("Set size: " + emptySet.size());
        System.out.println("Is empty: " + emptySet.isEmpty());
        
        try {
            emptySet.add("Attempt to add");
        } catch (UnsupportedOperationException e) {
            System.out.println("Expected exception: " + e.getMessage());
        }
    }
}

This code shows how to obtain an empty set using Collections.emptySet. We verify the set is empty by checking its size and isEmpty status. The attempt to add an element throws an UnsupportedOperationException.

The output demonstrates the set's immutability. This behavior is consistent with all immutable collection implementations in Java's Collections Framework.

Returning emptySet from Methods

A common use case for emptySet is returning empty results from methods. This example shows a method that conditionally returns an empty set. It's a cleaner alternative to returning null.

EmptySetReturn.java
package com.zetcode;

import java.util.Collections;
import java.util.Set;

public class EmptySetReturn {

    public static Set<String> getFilteredItems(boolean condition) {
        if (!condition) {
            return Collections.emptySet();
        }
        
        // In a real scenario, return populated set
        return Set.of("Item1", "Item2");
    }
    
    public static void main(String[] args) {
        Set<String> items = getFilteredItems(false);
        
        System.out.println("Returned set: " + items);
        System.out.println("Size: " + items.size());
    }
}

This example demonstrates using emptySet as a method return value. The method returns an empty set when a condition isn't met. This is preferable to returning null, as it avoids NullPointerException.

The caller can safely use the returned set without null checks. All set operations will work as expected on the empty set. This pattern is widely used in Java APIs.

emptySet in Collections Operations

emptySet can be used in various collection operations. This example shows using it with methods like addAll and containsAll. The empty set behaves predictably in these operations.

EmptySetOperations.java
package com.zetcode;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class EmptySetOperations {

    public static void main(String[] args) {
        
        Set<String> colors = new HashSet<>();
        colors.add("Red");
        colors.add("Green");
        
        Set<String> emptySet = Collections.emptySet();
        
        // AddAll with empty set
        boolean changed = colors.addAll(emptySet);
        System.out.println("After addAll: " + colors);
        System.out.println("Was modified? " + changed);
        
        // ContainsAll with empty set
        boolean containsAll = colors.containsAll(emptySet);
        System.out.println("Contains all empty? " + containsAll);
        
        // RemoveAll with empty set
        changed = colors.removeAll(emptySet);
        System.out.println("After removeAll: " + colors);
        System.out.println("Was modified? " + changed);
    }
}

This example shows how an empty set interacts with collection operations. Adding an empty set to another set has no effect. Any set contains all elements of an empty set. Removing an empty set also has no effect.

The output demonstrates these behaviors. The empty set serves as an identity element for many set operations. This makes it useful in algorithms and conditional logic.

Type-Safe emptySet Usage

The generic nature of emptySet ensures type safety. This example demonstrates using it with different types. The compiler enforces type constraints, preventing ClassCastException.

EmptySetTypes.java
package com.zetcode;

import java.util.Collections;
import java.util.Set;

public class EmptySetTypes {

    public static void main(String[] args) {
        
        Set<String> stringSet = Collections.emptySet();
        Set<Integer> intSet = Collections.emptySet();
        Set<Object> objectSet = Collections.emptySet();
        
        // All are truly empty
        System.out.println("String set size: " + stringSet.size());
        System.out.println("Integer set size: " + intSet.size());
        System.out.println("Object set size: " + objectSet.size());
        
        // Type safety demonstrated
        // stringSet.add(123); // Compile error
        // intSet.add("text"); // Compile error
    }
}

This example shows type-safe usage of emptySet. We create empty sets of different types. The compiler prevents adding wrong type elements, demonstrating type safety.

All sets are truly empty regardless of their generic type. The type parameter only affects compile-time checks. At runtime, they all reference the same immutable empty set instance.

emptySet in Method Parameters

emptySet can be used to pass empty collections to methods. This example shows a method that processes a set and how to call it with an empty set. This is cleaner than passing null.

EmptySetParameter.java
package com.zetcode;

import java.util.Collections;
import java.util.Set;

public class EmptySetParameter {

    public static void processSet(Set<String> items) {
        System.out.println("Processing set with " + items.size() + " items");
        
        for (String item : items) {
            System.out.println("Processing: " + item);
        }
    }
    
    public static void main(String[] args) {
        // Passing empty set to method
        processSet(Collections.emptySet());
        
        // Compare with null
        try {
            processSet(null);
        } catch (NullPointerException e) {
            System.out.println("Null caused: " + e);
        }
    }
}

This example demonstrates passing an empty set to a method. The method works correctly with the empty set, iterating zero times. Passing null, by contrast, causes a NullPointerException.

Using emptySet makes APIs more robust. Methods don't need null checks when empty sets are used instead. This leads to cleaner, more reliable code.

emptySet vs. new HashSet

This example compares emptySet with creating new empty HashSets. It demonstrates the memory and performance advantages of using the immutable empty set.

EmptySetVsHashSet.java
package com.zetcode;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class EmptySetVsHashSet {

    public static void main(String[] args) {
        
        Set<String> emptySet1 = Collections.emptySet();
        Set<String> emptySet2 = Collections.emptySet();
        
        Set<String> hashSet1 = new HashSet<>();
        Set<String> hashSet2 = new HashSet<>();
        
        // Same instance check
        System.out.println("emptySet1 == emptySet2: " + 
            (emptySet1 == emptySet2));
        System.out.println("hashSet1 == hashSet2: " + 
            (hashSet1 == hashSet2));
        
        // Memory comparison
        System.out.println("emptySet1 size estimate: " + 
            estimateSize(emptySet1));
        System.out.println("hashSet1 size estimate: " + 
            estimateSize(hashSet1));
    }
    
    private static int estimateSize(Set<?> set) {
        // Simplified size estimation
        return set instanceof HashSet ? 16 : 0;
    }
}

This example compares the immutable empty set with newly created HashSets. The empty set is a singleton, so all calls to emptySet return the same instance. New HashSets are distinct objects.

The output shows the memory advantage. The empty set uses no additional memory for elements. A HashSet, even when empty, allocates memory for its internal structures.

emptySet in Stream Operations

emptySet works well with Java Streams. This example shows using it as a default value in stream operations. It provides a safe, immutable result when no elements are present.

EmptySetStreams.java
package com.zetcode;

import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class EmptySetStreams {

    public static void main(String[] args) {
        
        // Stream with elements
        Set<String> colors = Stream.of("Red", "Green", "Blue")
            .filter(s -> s.startsWith("B"))
            .collect(Collectors.toSet());
        System.out.println("Colors starting with B: " + colors);
        
        // Empty stream case
        Set<String> empty = Stream.<String>empty()
            .collect(Collectors.toSet());
        System.out.println("Collected empty stream: " + empty);
        
        // Using emptySet as default
        Set<String> safeResult = colors.isEmpty() ? 
            Collections.emptySet() : colors;
        System.out.println("Safe result: " + safeResult);
    }
}

This example demonstrates emptySet in stream processing. We collect stream results into sets and handle empty cases. The empty set serves as a safe default value.

The output shows how streams naturally produce empty sets when no elements match. Using emptySet explicitly makes the code's intent clearer in some cases.

Source

Java Collections.emptySet Documentation

In this article, we've explored Collections.emptySet in depth. We've covered basic usage, method returns, collection operations, type safety, and stream integration. The empty set is a simple but powerful tool in Java.

Author

My name is Jan Bodnar, and I am a dedicated programmer with many years of experience in the field. I began writing programming articles in 2007 and have since authored over 1,400 articles and eight e-books. With more than eight years of teaching experience, I am committed to sharing my knowledge and helping others master programming concepts.

List all Java tutorials.