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:
// 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 is: " + set); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } } } |
Exception thrown : java.lang.IllegalArgumentException: Map is non-empty
Recommended Posts:
- Collections max() method in Java with Examples
- Collections min() method in Java with Examples
- Collections checkedList() method in Java with Examples
- Collections copy() method in Java with Examples
- Collections checkedSortedSet() method in Java with Examples
- Collections fill() method in Java with Examples
- Collections singletonMap() method in Java with Examples
- Collections checkedSet() method in Java with Examples
- Collections synchronizedSortedMap() method in Java with Examples
- Collections singletonList() method in Java with Examples
- Collections addAll() method in Java with Examples
- Collections enumeration() method in Java with Examples
- Collections synchronizedSortedSet() method in Java with Examples
- Collections checkedSortedMap() method in Java with Examples
- Collections asLifoQueue() 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.



