- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Collections newSetFromMap() Method
Description
The Java Collections newSetFromMap(Map<E, Boolean>) method is used to return a set backed by the specified map.
Declaration
Following is the declaration for java.util.Collections.newSetFromMap() method.
public static <E> Set<E> newSetFromMap(Map<E, Boolean> map)
Parameters
map − The backing map
Return Value
The method call returns the set backed by the map.
Exception
IllegalArgumentException − This is thrown if map is not empty.
Getting a Set of Strings Backed by Map Example
The following example shows the usage of Java Collection newSetFromMap(Map) method. We've created a Map object of String and Boolean. Then a set is created using newSetFromMap(Map) method. Values are added to set and then both set and map are printed.
package com.tutorialspoint; import java.util.Collections; import java.util.Map; import java.util.Set; import java.util.WeakHashMap; public class CollectionsDemo { public static void main(String args[]) { // create map Map<String, Boolean> map = new WeakHashMap<String, Boolean>(); // create a set from map Set<String> set = Collections.newSetFromMap(map); // add values in set set.add("Java"); set.add("C"); set.add("C++"); // set and map values are System.out.println("Set is: " + set); System.out.println("Map is: " + map); } }
Let us compile and run the above program, this will produce the following result.
Set is: [Java, C++, C] Map is: {Java=true, C++=true, C=true}
Getting a Set of Integers Backed by Map Example
The following example shows the usage of Java Collection newSetFromMap(Map) method. We've created a Map object of Integer and Boolean. Then a set is created using newSetFromMap(Map) method. Values are added to set and then both set and map are printed.
package com.tutorialspoint; import java.util.Collections; import java.util.Map; import java.util.Set; import java.util.WeakHashMap; public class CollectionsDemo { public static void main(String args[]) { // create map Map<Integer, Boolean> map = new WeakHashMap<Integer, Boolean>(); // create a set from map Set<Integer> set = Collections.newSetFromMap(map); // add values in set set.add(1); set.add(2); set.add(3); // set and map values are System.out.println("Set is: " + set); System.out.println("Map is: " + map); } }
Let us compile and run the above program, this will produce the following result.
Set is: [3, 2, 1] Map is: {3=true, 2=true, 1=true}
Getting a Set of Objects Backed by Map Example
The following example shows the usage of Java Collection newSetFromMap(Map) method. We've created a Map object of Student object and Boolean. Then a set is created using newSetFromMap(Map) method. Values are added to set and then both set and map are printed.
package com.tutorialspoint; import java.util.Collections; import java.util.Map; import java.util.Set; import java.util.WeakHashMap; public class CollectionsDemo { public static void main(String args[]) { // create map Map<Student, Boolean> map = new WeakHashMap<Student, Boolean>(); // create a set from map Set<Student> set = Collections.newSetFromMap(map); // add values in set set.add(new Student(1, "Julie")); set.add(new Student(2, "Robert")); set.add(new Student(3, "Adam")); // set and map values are System.out.println("Set is: " + set); System.out.println("Map is: " + map); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } }
Let us compile and run the above program, this will produce the following result.
Set is: [[ 1, Julie ], [ 3, Adam ], [ 2, Robert ]] Map is: {[ 1, Julie ]=true, [ 3, Adam ]=true, [ 2, Robert ]=true}
To Continue Learning Please Login
Login with Google