Java Collection Tutorial - Java Collections.newSetFromMap(Map <E, Boolean > map)








Syntax

Collections.newSetFromMap(Map <E, Boolean > map) has the following syntax.

public static <E> Set <E> newSetFromMap(Map <E, Boolean> map)

Example

In the following code shows how to use Collections.newSetFromMap(Map <E, Boolean > map) method.

import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
//from   w  w w. j a  v  a  2 s . c om
public class Main {
   public static void main(String args[]) { 
 
      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.add("from java2s.com");
      
      // set and map values are
      System.out.println("Set is: " + set);
      System.out.println("Map is: " + map);
   }
}

The code above generates the following result.