Class ManyToManyMap<L,​R>


  • public class ManyToManyMap<L,​R>
    extends Object
    Maps keys to lists of values and values to lists of keys. The whole concept of key value is a bit vague here because each value is also a key. Consider the following example: A maps to B and C, B maps to D. get(A) would return B and C, get(B) would return A and D, get(C) would return A, get(D) would return B. Each mapping is bidirectional.
    Author:
    marrink
    • Constructor Detail

      • ManyToManyMap

        public ManyToManyMap()
        Creates map with default initial size and load factor.
      • ManyToManyMap

        public ManyToManyMap​(int initialCapacity)
        Creates map with default load factor and specified initial size.
        Parameters:
        initialCapacity -
      • ManyToManyMap

        public ManyToManyMap​(int initialCapacity,
                             float loadFactor)
        Creates map with specified initial size and load factor. For more information about these see HashMap
        Parameters:
        initialCapacity -
        loadFactor -
    • Method Detail

      • add

        public void add​(L left,
                        R right)
        Adds a key value mapping in this map. Since this maps many to many relations no previous mappings will be overridden. The size of the map may or may not change depending on whether both objects are already present or not
        Parameters:
        left -
        right -
      • remove

        public boolean remove​(L left,
                              R right)
        Removes a many to many mapping between two objects. The size of the map may or may not change depending on on whether or not both objects have other mappings.
        Parameters:
        left - left side of the mapping
        right - right side of the mapping
        Returns:
        false if the mapping did not exist, true otherwise
      • removeAllMappingsForLeft

        public Set<R> removeAllMappingsForLeft​(L left)
        Remove all mappings for an object. The size of the map will at least decrease by one (if the object is present) but possibly more.
        Parameters:
        left - the left side of the many to many mapping
        Returns:
        the mappings that will be removed by this action
      • removeAllMappingsForRight

        public Set<L> removeAllMappingsForRight​(R right)
        Remove all mappings for an object. The size of the map will at least decrease by one (if the object is present) but possibly more.
        Parameters:
        right - the right side of the many to many mapping
        Returns:
        the mappings that will be removed by this action
      • getRight

        public Set<R> getRight​(L left)
        Gets the bidirectional mappings for this object.
        Parameters:
        left -
        Returns:
        the many to many mappings for this object
      • getLeft

        public Set<L> getLeft​(R right)
        Gets the bidirectional mappings for this object.
        Parameters:
        right -
        Returns:
        the many to many mappings for this object
      • size

        public int size()
        Returns the number of mapped values, left or right
        Returns:
        the number of mapped values
      • numberOfmappingsForLeft

        public int numberOfmappingsForLeft​(L left)
        Returns the number of keys mapped to a value.
        Parameters:
        left -
        Returns:
        the number of keys mapped to this value
      • numberOfmappingsForRight

        public int numberOfmappingsForRight​(R right)
        Returns the number of keys mapped to a value.
        Parameters:
        right -
        Returns:
        the number of keys mapped to this value
      • containsLeft

        public boolean containsLeft​(L left)
        Check if this map contains a key.
        Parameters:
        left - a mapped object
        Returns:
        true if this map contains the key, false otherwise
      • containsRight

        public boolean containsRight​(R right)
        Check if this map contains a key.
        Parameters:
        right - a mapped object
        Returns:
        true if this map contains the key, false otherwise
      • isEmpty

        public boolean isEmpty()
        Check if this map contains any mappings. If this map does is empty size will be 0.
        Returns:
        true if no mappings are present, false otherwise
      • clear

        public void clear()
        Removes all mappings.
      • leftIterator

        public Iterator<L> leftIterator()
        Returns an Iterator over every left hand mapping in this map. In no particular order.
        Returns:
        an iterator over this map
      • rightIterator

        public Iterator<R> rightIterator()
        Returns an Iterator over every rightt hand mapping in this map. In no particular order.
        Returns:
        an iterator over this map