Class SetMap<K,V>

java.lang.Object
com.pnfsoftware.jeb.util.collect.SetMap<K,V>
Type Parameters:
K - key type
V - type of value to be stored

@Ser public class SetMap<K,V> extends Object
A map of keys to sets. Keys may be ordered (none, natural, by insertion); the ordering for the value sets may be specified as well. This class is serializable. Null key is allowed except for natural ordering; null values are allowed.

Thread-safety: none.

Note: this class does not implement the Map interface, but attempts to follow its guidelines and contracts the best it can. One notable exception is that this class does not override hashCode/equals.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new set-map without any particular ordering.
    SetMap(CollectionOrder keyOrder, CollectionOrder setOrder)
    Create a new set-map.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Remove all key-value mappings.
    boolean
    Determine whether this set-map contains a key.
    boolean
    Determine whether this set-map contains a value.
    boolean
    createKey(K key)
    Create an explicit entry for a given key.
    get(K key)
    Retrieve the values associated with a key.
    get(K key, boolean safe)
    Retrieve the values associated with a key.
    getSafe(K key)
    Retrieve the values associated with a key.
    boolean
    Determine whether this set-map has no values.
    Retrieve a read-only view of the keys.
    int
    Get the number of keys in this set-map.
    int
    put(K key, V value)
    Add a key-value mapping.
    void
    putAll(Map<? extends K,List<? extends V>> m)
    Add all mappings from a map of keys to lists of values.
    void
    putMulti(K key, Collection<? extends V> values)
    Add multiple elements mapped by the same key.
    remove(K key)
    Remove all values associated with a key.
    boolean
    Remove all entries mapping to the collection of keys.
    int
    removeMulti(K key, Collection<? extends V> values)
    Remove multiple values associated with a key.
    removeValue(K key, V value)
    Remove a value associated with a key.
    int
    Get the number of key-value mappings (per Map interface).
     
    Retrieve all distinct values.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • SetMap

      public SetMap()
      Create a new set-map without any particular ordering.
    • SetMap

      public SetMap(CollectionOrder keyOrder, CollectionOrder setOrder)
      Create a new set-map.
      Parameters:
      keyOrder - key ordering
      setOrder - value-set ordering
  • Method Details

    • clear

      public void clear()
      Remove all key-value mappings.
    • keySize

      public int keySize()
      Get the number of keys in this set-map.
      Returns:
      number of keys, including keys mapped to empty value sets
    • isEmpty

      public boolean isEmpty()
      Determine whether this set-map has no values.
      Returns:
      true if no values are mapped
    • size

      public int size()
      Get the number of key-value mappings (per Map interface). That is, the number of values.
      Returns:
      number of values
    • put

      public int put(K key, V value)
      Add a key-value mapping.
      Parameters:
      key - key
      value - value
      Returns:
      the updated count of values associated with the key
    • putMulti

      public void putMulti(K key, Collection<? extends V> values)
      Add multiple elements mapped by the same key. Note that if an empty collection is provided, the entry for the provided key will not be be explicitly created.
      Parameters:
      key - key
      values - values to add
    • putAll

      public void putAll(Map<? extends K,List<? extends V>> m)
      Add all mappings from a map of keys to lists of values.
      Parameters:
      m - map of values to add
    • createKey

      public boolean createKey(K key)
      Create an explicit entry for a given key. If no values mapped by the given key previously existed, the associated value will be an empty collection.
      Parameters:
      key - key
      Returns:
      true if the map did not already contain the key; false otherwise (the key entry existed before)
    • containsKey

      public boolean containsKey(K key)
      Determine whether this set-map contains a key.
      Parameters:
      key - key
      Returns:
      true if the key is present
    • containsValue

      public boolean containsValue(V value)
      Determine whether this set-map contains a value.
      Parameters:
      value - value
      Returns:
      true if any key maps to the value
    • get

      public Set<V> get(K key)
      Retrieve the values associated with a key.
      Parameters:
      key - key
      Returns:
      read-only set of values, or null if the key is absent
    • get

      public Set<V> get(K key, boolean safe)
      Retrieve the values associated with a key.
      Parameters:
      key - key
      safe - if true, an empty collection of values is returned if the key does not exist
      Returns:
      read-only set of values, possibly null or empty depending on safe
    • getSafe

      public Set<V> getSafe(K key)
      Retrieve the values associated with a key.
      Parameters:
      key - key
      Returns:
      a non-null set of values
    • remove

      public Set<V> remove(K key)
      Remove all values associated with a key.
      Parameters:
      key - key
      Returns:
      removed values, or null if the key was absent
    • removeValue

      public V removeValue(K key, V value)
      Remove a value associated with a key.
      Parameters:
      key - key
      value - value to remove
      Returns:
      removed value, or null if no value was removed
    • removeMulti

      public int removeMulti(K key, Collection<? extends V> values)
      Remove multiple values associated with a key.
      Parameters:
      key - key
      values - values to remove
      Returns:
      number of removed values
    • values

      public Set<V> values()
      Retrieve all distinct values.
      Returns:
      read-only set of values
    • keySet

      public Set<K> keySet()
      Retrieve a read-only view of the keys.

      WARNING: This method does not respect the Map.keySet() contract: the set returned is read-only.

      Returns:
      read-only view of the key set
    • removeAll

      public boolean removeAll(Collection<K> keys)
      Remove all entries mapping to the collection of keys.

      Similar semantics as Set.removeAll(Collection).

      Parameters:
      keys - keys to remove
      Returns:
      true if the multi-map was modified
    • toString

      public String toString()
      Overrides:
      toString in class Object