Collections newSetFromMap() method in Java with Examples Last Updated : 25 Oct, 2019 Comments Improve Suggest changes Like Article Like Report The newSetFromMap() method of java.util.Collections class is used to return a set backed by the specified map. The resulting set displays the same ordering, concurrency, and performance characteristics as the backing map. In essence, this factory method provides a Set implementation corresponding to any Map implementation. There is no need to use this method on a Map implementation that already has a corresponding Set implementation (such as HashMap or TreeMap). Syntax: public static Set newSetFromMap(Map map) Parameters: This method takes the backing map as a parameter Return Value: This method returns the set backed by the map Exception: This method throws IllegalArgumentException, if map is not empty. Below are the examples to illustrate the newSetFromMap() method Example 1: Java // Java program to demonstrate // newSetFromMap() method import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // creating object of Map<String, Boolean> Map<String, Boolean> map = new WeakHashMap<String, Boolean>(); // creating object of LinkedList Set<String> set = Collections.newSetFromMap(map); // add values in set set.add("A"); set.add("B"); set.add("C"); set.add("D"); // set and map values are System.out.println("Map is: " + map); System.out.println("Set from Map is: " + set); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } } } Output: Map is: {C=true, B=true, A=true, D=true} Set from Map is: [C, B, A, D] Example 2: for IllegalArgumentException Java // Java program to demonstrate // newSetFromMap() method // for IllegalArgumentException import java.util.*; <div class="code-output"> <b>Output:</b> <pre> Set is: [C, B, A, D] Map is: {C=true, B=true, A=true, D=true} </pre> </div> public class GFG1 { public static void main(String[] argv) throws Exception { try { // creating object of Map<String, Boolean> Map<String, Boolean> map = new WeakHashMap<String, Boolean>(); // putting value in map map.put("1", true); // creating object of LinkedList Set<String> set = Collections.newSetFromMap(map); // add values in set set.add("A"); set.add("B"); set.add("C"); set.add("D"); // set and map values are System.out.println("Map is: " + map); System.out.println("Set from Map is: " + set); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } } } Output: Exception thrown : java.lang.IllegalArgumentException: Map is non-empty Comment More infoAdvertise with us Next Article Collections newSetFromMap() method in Java with Examples rohitprasad3 Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions +1 More Practice Tags : JavaJava-CollectionsMisc Similar Reads Collections singletonMap() method in Java with Examples The singletonMap() method of java.util.Collections class is used to return an immutable map, mapping only the specified key to the specified value. The returned map is serializable. Syntax: public static Map singletonMap(K key, V value) Parameters: This method takes the following parameters as a arg 2 min read Collections synchronizedMap() method in Java with Examples The synchronizedMap() method of java.util.Collections class is used to return a synchronized (thread-safe) map backed by the specified map. In order to guarantee serial access, it is critical that all access to the backing map is accomplished through the returned map. Syntax: public static <K, V 2 min read Collections unmodifiableMap() method in Java with Examples The unmodifiableMap() method of java.util.Collections class is used to return an unmodifiable view of the specified map. This method allows modules to provide users with "read-only" access to internal maps. Query operations on the returned map "read through" to the specified map, and attempts to mod 2 min read Collections unmodifiableSortedMap() method in Java with Examples The unmodifiableSortedMap() method of java.util.Collections class is used to return an unmodifiable view of the specified sorted map. This method allows modules to provide users with "read-only" access to internal sorted maps. Query operations on the returned sorted map "read through" to the specifi 2 min read Collections synchronizedSortedMap() method in Java with Examples The synchronizedSortedMap() method of java.util.Collections class is used to return a synchronized (thread-safe) sorted map backed by the specified sorted map. In order to guarantee serial access, it is critical that all access to the backing sorted map is accomplished through the returned sorted ma 2 min read Like