Home Java Collections Methods newSetFromMap() Java Collections newSetFromMap() Method Definition In Java, Collections.newSetFromMap(map) returns a Set whose elements are stored as the keys of map (every value is Boolean.TRUE). The set behaves exactly like the map does: same ordering, same thread-safety, same rules about null.
It exists for maps that have no matching Set class, the classic case being ConcurrentHashMap, which gives you a thread-safe hash set.
A static method of the java.util.Collections utility class, available since Java 6. Call it on the class name, as in Collections.newSetFromMap(...), after import java.util.Collections;. You never create a Collections object.
Syntax Pass a new, empty map with Boolean values.
Set<String> safe = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
Set<String> ordered = Collections.newSetFromMap(new LinkedHashMap<String, Boolean>()); Call What it does newSetFromMap(new ConcurrentHashMap<>())Thread-safe set with weakly consistent iteration. newSetFromMap(new LinkedHashMap<>())Insertion-ordered set (like LinkedHashSet). newSetFromMap(new WeakHashMap<>())Set whose elements can be garbage-collected. ConcurrentHashMap.newKeySet()Java 8+ shortcut for a concurrent set.
Added in Java 6. Since Java 8, ConcurrentHashMap.newKeySet() is a shorter way to get a concurrent set, but newSetFromMap works with any map type.
Library declarations // java.util.Collections
public static <E> Set<E> newSetFromMap(Map<E, Boolean> map) Parameters Parameter Description mapAn empty map that will hold the set's elements as keys. Do not use it directly afterwards.
Return Value Return Value Description Set<E>A set backed by map.
Important
If the map is not empty, the method throws IllegalArgumentException. After creating the set, work only through the set; the map is its private storage. Null handling follows the map, so a ConcurrentHashMap-backed set throws NullPointerException for add(null).
Compare with Similar Methods Other ways to get special sets:
Java Examples 1. Insertion order 2. Concurrent set 3. Empty map rule Example 1 — An Insertion-Ordered Set from LinkedHashMap Add words with a duplicate and watch both the set and its backing map.
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class CollectionsNewSetFromMapOrdered {
public static void main(String[] args) {
Map<String, Boolean> backing = new LinkedHashMap<>();
Set<String> seen = Collections.newSetFromMap(backing);
for (String s : Arrays.asList("banana", "apple", "banana", "cherry")) {
System.out.println("add " + s + ": " + seen.add(s));
}
System.out.println("set: " + seen);
System.out.println("map: " + backing);
}
} add banana: true
add apple: true
add banana: false
add cherry: true
set: [banana, apple, cherry]
map: {banana=true, apple=true, cherry=true} How It Works add = put. Each add puts the word into the map with value true and reports whether the key was new.Duplicate rejected. The second "banana" is already a key, so add returns false.Map decides order. LinkedHashMap keeps insertion order, so the set prints banana, apple, cherry, and the map shows every value as true.Example 2 — A ConcurrentHashMap-Backed Set Remove items while looping over the set (which would fail on a HashSet), then try to add null.
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
public class CollectionsNewSetFromMapConcurrent {
public static void main(String[] args) {
Set<String> online = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
online.addAll(Arrays.asList("ana", "tmp1", "bo", "tmp2"));
for (String user : online) {
if (user.startsWith("tmp")) {
online.remove(user);
}
}
System.out.println("left: " + new TreeSet<>(online) + " size " + online.size());
try {
online.add(null);
} catch (NullPointerException e) {
System.out.println("add(null): " + e.getClass().getSimpleName());
}
}
} left: [ana, bo] size 2
add(null): NullPointerException How It Works Weakly consistent iteration. ConcurrentHashMap iterators never throw ConcurrentModificationException, so removing during the loop is safe.Stable printing. Hash order is not predictable, so the code prints a sorted TreeSet copy: the two temporary users are gone.Map rules apply. ConcurrentHashMap forbids null keys, so the set's add(null) throws NullPointerException.Example 3 — The Map Must Start Empty Pass a non-empty map, then build a descending set from a TreeMap with a reverse comparator.
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class CollectionsNewSetFromMapRules {
public static void main(String[] args) {
Map<String, Boolean> used = new TreeMap<>();
used.put("x", true);
try {
Collections.newSetFromMap(used);
} catch (IllegalArgumentException e) {
System.out.println("non-empty map: " + e.getClass().getSimpleName());
}
Map<String, Boolean> desc = new TreeMap<String, Boolean>(Collections.<String>reverseOrder());
Set<String> letters = Collections.newSetFromMap(desc);
letters.addAll(Arrays.asList("b", "c", "a"));
System.out.println("descending set: " + letters);
}
} non-empty map: IllegalArgumentException
descending set: [c, b, a] How It Works Guard. The map already holds "x", so newSetFromMap refuses it with IllegalArgumentException.Any map works. A TreeMap built with reverseOrder() is empty and accepted.Ordering inherited. The letters are stored as tree keys, so the set iterates in descending order: c, b, a.Frequently Asked Questions What does Collections.newSetFromMap() do? It returns a Set that stores its elements as the keys of the map you pass, so the set gets that map's ordering and concurrency behavior.
Why does newSetFromMap() throw IllegalArgumentException? The map you pass must be empty. A map that already has entries is rejected.
How do I create a concurrent Set in Java? Collections.newSetFromMap(new ConcurrentHashMap<>()) works since Java 6; ConcurrentHashMap.newKeySet() is a Java 8+ shortcut.
Can I keep using the map after calling newSetFromMap()? You should not. Treat the map as the set's internal storage and make all changes through the set.
Which Java version added newSetFromMap()? Java 6.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
13 people found this page helpful
Helpful Share Copy link Suggestion