Java Collections Java Java API
Class:
java.util.Collections
Method:
public static <E> Set<E> newSetFromMap(Map<E,Boolean> map)
Returns a set backed by the specified map. The resulting set displays the same ordering, concurrency, and performance characteristics as the backing map. It is useful if we already do not have a required Set implementation but a Map exits having the same characteristics with its keys.
Each method invocation on the set returned by this method results in exactly one method invocation on the backing map or its keySet view, with one exception. The addAll method is implemented as a sequence of put invocations on the backing map.
Examples
package com.logicbig.example.collections;
import java.util.*;
public class NewSetFromMapExample {
public static void main(String... args) { Map<Integer, Boolean> map = new IdentityHashMap<>(); Set<Integer> set = Collections.newSetFromMap(map); Integer a = new Integer(1); Integer b = new Integer(1); Integer c = new Integer(1); set.add(a); set.add(b); set.add(c); System.out.println(set);
Set<Integer> set2 = new HashSet<>(); set2.add(a); set2.add(b); set2.add(c); System.out.println(set2); } }
Output[1, 1, 1] [1]
|