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}