Collections newSetFromMap() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Map is: {C=true, B=true, A=true, D=true}
Set from Map is: [C, B, A, D]

Example 2: for IllegalArgumentException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Exception thrown : java.lang.IllegalArgumentException: Map is non-empty

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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