Class MapBackedSet<E,​V>

  • Type Parameters:
    E - the type of the elements in this set
    V - the dummy value type in this map
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.Set<E>

    public final class MapBackedSet<E,​V>
    extends java.lang.Object
    implements java.util.Set<E>, java.io.Serializable
    Decorates a Map to obtain Set behaviour.

    This class is used to create a Set with the same properties as the key set of any map. Thus, a ReferenceSet can be created by wrapping a ReferenceMap in an instance of this class.

    Most map implementation can be used to create a set by passing in dummy values. Exceptions include BidiMap implementations, as they require unique values.

    Since:
    3.1
    See Also:
    Serialized Form
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(E obj)  
      boolean addAll​(java.util.Collection<? extends E> coll)  
      void clear()  
      boolean contains​(java.lang.Object obj)  
      boolean containsAll​(java.util.Collection<?> coll)  
      boolean equals​(java.lang.Object obj)  
      int hashCode()  
      boolean isEmpty()  
      java.util.Iterator<E> iterator()  
      static <E,​V>
      MapBackedSet<E,​V>
      mapBackedSet​(java.util.Map<E,​? super V> map)
      Factory method to create a set from a map.
      static <E,​V>
      MapBackedSet<E,​V>
      mapBackedSet​(java.util.Map<E,​? super V> map, V dummyValue)
      Factory method to create a set from a map.
      boolean remove​(java.lang.Object obj)  
      boolean removeAll​(java.util.Collection<?> coll)  
      boolean removeIf​(java.util.function.Predicate<? super E> filter)  
      boolean retainAll​(java.util.Collection<?> coll)  
      int size()  
      java.lang.Object[] toArray()  
      <T> T[] toArray​(T[] array)  
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.Set

        spliterator
    • Method Detail

      • mapBackedSet

        public static <E,​V> MapBackedSet<E,​V> mapBackedSet​(java.util.Map<E,​? super V> map)
        Factory method to create a set from a map.
        Type Parameters:
        E - the element type
        V - the dummy value type in the map
        Parameters:
        map - the map to decorate, must not be null
        Returns:
        a new map backed set
        Throws:
        java.lang.NullPointerException - if map is null
        Since:
        4.0
      • mapBackedSet

        public static <E,​V> MapBackedSet<E,​V> mapBackedSet​(java.util.Map<E,​? super V> map,
                                                                       V dummyValue)
        Factory method to create a set from a map.
        Type Parameters:
        E - the element type
        V - the dummy value type in the map
        Parameters:
        map - the map to decorate, must not be null
        dummyValue - the dummy value to use
        Returns:
        a new map backed set
        Throws:
        java.lang.NullPointerException - if map is null
        Since:
        4.0
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<E>
        Specified by:
        size in interface java.util.Set<E>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<E>
        Specified by:
        isEmpty in interface java.util.Set<E>
      • iterator

        public java.util.Iterator<E> iterator()
        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Specified by:
        iterator in interface java.util.Set<E>
      • contains

        public boolean contains​(java.lang.Object obj)
        Specified by:
        contains in interface java.util.Collection<E>
        Specified by:
        contains in interface java.util.Set<E>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> coll)
        Specified by:
        containsAll in interface java.util.Collection<E>
        Specified by:
        containsAll in interface java.util.Set<E>
      • add

        public boolean add​(E obj)
        Specified by:
        add in interface java.util.Collection<E>
        Specified by:
        add in interface java.util.Set<E>
      • addAll

        public boolean addAll​(java.util.Collection<? extends E> coll)
        Specified by:
        addAll in interface java.util.Collection<E>
        Specified by:
        addAll in interface java.util.Set<E>
      • remove

        public boolean remove​(java.lang.Object obj)
        Specified by:
        remove in interface java.util.Collection<E>
        Specified by:
        remove in interface java.util.Set<E>
      • removeIf

        public boolean removeIf​(java.util.function.Predicate<? super E> filter)
        Specified by:
        removeIf in interface java.util.Collection<E>
        Since:
        4.4
      • removeAll

        public boolean removeAll​(java.util.Collection<?> coll)
        Specified by:
        removeAll in interface java.util.Collection<E>
        Specified by:
        removeAll in interface java.util.Set<E>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> coll)
        Specified by:
        retainAll in interface java.util.Collection<E>
        Specified by:
        retainAll in interface java.util.Set<E>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<E>
        Specified by:
        clear in interface java.util.Set<E>
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<E>
        Specified by:
        toArray in interface java.util.Set<E>
      • toArray

        public <T> T[] toArray​(T[] array)
        Specified by:
        toArray in interface java.util.Collection<E>
        Specified by:
        toArray in interface java.util.Set<E>
      • equals

        public boolean equals​(java.lang.Object obj)
        Specified by:
        equals in interface java.util.Collection<E>
        Specified by:
        equals in interface java.util.Set<E>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Collection<E>
        Specified by:
        hashCode in interface java.util.Set<E>
        Overrides:
        hashCode in class java.lang.Object