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 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); } } } |
Map is: {C=true, B=true, A=true, D=true}
Set from Map is: [C, B, A, D]
Example 2: for IllegalArgumentException
// Java program to demonstrate // newSetFromMap() method // for IllegalArgumentException import java.util.*; <div class = 'outputDiv'> <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); } } } |
Exception thrown : java.lang.IllegalArgumentException: Map is non-empty
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Collections max() method in Java with Examples
- Collections min() method in Java with Examples
- Collections synchronizedMap() method in Java with Examples
- Collections synchronizedList() method in Java with Examples
- Collections swap() method in Java with Examples
- Collections replaceAll() method in Java with Examples
- Collections synchronizedSet() method in Java with Examples
- Collections synchronizedSortedMap() method in Java with Examples
- Collections checkedMap() method in Java with Examples
- Collections singletonList() method in Java with Examples
- Collections synchronizedCollection() method in Java with Examples
- Collections singletonMap() method in Java with Examples
- Collections list() method in Java with Examples
- Collections unmodifiableList() method in Java with Examples
- Collections unmodifiableMap() method in Java with Examples
- Collections unmodifiableSet() method in Java with Examples
- Collections unmodifiableSortedMap() method in Java with Examples
- Collections unmodifiableCollection() method in Java with Examples
- Collections asLifoQueue() method in Java with Examples
- Collections addAll() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai

