Class HashBag<T>

All Implemented Interfaces:
Externalizable, Serializable, Iterable<T>, Collection<T>, Bag<T>, MutableBag<T>, MutableBagIterable<T>, UnsortedBag<T>, MutableCollection<T>, InternalIterable<T>, RichIterable<T>

public class HashBag<T>
extends AbstractHashBag<T>
implements Externalizable
A HashBag is a MutableBag which uses a Map as its underlying data store. Each key in the Map represents some item, and the value in the map represents the current number of occurrences of that item.
Since:
1.0
See Also:
Serialized Form
  • Constructor Details

  • Method Details

    • newBag

      public static <E> HashBag<E> newBag()
    • newBag

      public static <E> HashBag<E> newBag​(int size)
    • newBag

      public static <E> HashBag<E> newBag​(Bag<? extends E> source)
    • newBag

      public static <E> HashBag<E> newBag​(Iterable<? extends E> source)
    • newBagWith

      public static <E> HashBag<E> newBagWith​(E... elements)
    • selectByOccurrences

      public MutableBag<T> selectByOccurrences​(IntPredicate predicate)
      Description copied from interface: Bag
      Returns all elements of the bag that have a number of occurrences that satisfy the predicate.
      Specified by:
      selectByOccurrences in interface Bag<T>
      Specified by:
      selectByOccurrences in interface MutableBag<T>
      Specified by:
      selectByOccurrences in interface MutableBagIterable<T>
      Specified by:
      selectByOccurrences in interface UnsortedBag<T>
    • writeExternal

      public void writeExternal​(ObjectOutput out) throws IOException
      Specified by:
      writeExternal in interface Externalizable
      Throws:
      IOException
    • readExternal

      public void readExternal​(ObjectInput in) throws IOException, ClassNotFoundException
      Specified by:
      readExternal in interface Externalizable
      Throws:
      IOException
      ClassNotFoundException
    • without

      public HashBag<T> without​(T element)
      Description copied from interface: MutableCollection
      This method allows mutable and fixed size collections the ability to remove elements from their existing elements. In order to support fixed size a new instance of a collection would have to be returned containing the elements that would be left from the original collection after calling remove. In the case of mutable collections, the original collection is modified, and is returned. In order to use this method properly with mutable and fixed size collections the following approach must be taken:
       MutableCollection<String> list = list.without("1");
       list = list.without("2");
       return list;
       
      In the case of FixedSizeCollection a new instance of MutableCollection will be returned by without, and any variables that previously referenced the original collection will need to be redirected to reference the new instance. For other MutableCollection types you will replace the reference to collection with the same collection, since the instance will return "this" after calling remove on itself.
      Specified by:
      without in interface MutableBag<T>
      Specified by:
      without in interface MutableBagIterable<T>
      Specified by:
      without in interface MutableCollection<T>
      See Also:
      Collection.remove(Object)
    • newEmpty

      public MutableBag<T> newEmpty()
      Description copied from interface: MutableCollection
      Creates a new empty mutable version of the same collection type. For example, if this instance is a FastList, this method will return a new empty FastList. If the class of this instance is immutable or fixed size (i.e. SingletonList) then a mutable alternative to the class will be provided.
      Specified by:
      newEmpty in interface MutableBag<T>
      Specified by:
      newEmpty in interface MutableCollection<T>
    • with

      public HashBag<T> with​(T element)
      Description copied from interface: MutableCollection
      This method allows mutable and fixed size collections the ability to add elements to their existing elements. In order to support fixed size a new instance of a collection would have to be returned taking the elements of the original collection and appending the new element to form the new collection. In the case of mutable collections, the original collection is modified, and is returned. In order to use this method properly with mutable and fixed size collections the following approach must be taken:
       MutableCollection<String> list = list.with("1");
       list = list.with("2");
       return list;
       
      In the case of FixedSizeCollection a new instance of MutableCollection will be returned by with, and any variables that previously referenced the original collection will need to be redirected to reference the new instance. For other MutableCollection types you will replace the reference to collection with the same collection, since the instance will return "this" after calling add on itself.
      Specified by:
      with in interface MutableBag<T>
      Specified by:
      with in interface MutableBagIterable<T>
      Specified by:
      with in interface MutableCollection<T>
      See Also:
      Collection.add(Object)
    • withAll

      public HashBag<T> withAll​(Iterable<? extends T> iterable)
      Description copied from interface: MutableCollection
      This method allows mutable and fixed size collections the ability to add multiple elements to their existing elements. In order to support fixed size a new instance of a collection would have to be returned taking the elements of the original collection and appending the new elements to form the new collection. In the case of mutable collections, the original collection is modified, and is returned. In order to use this method properly with mutable and fixed size collections the following approach must be taken:
       MutableCollection<String> list = list.withAll(FastList.newListWith("1", "2"));
       
      In the case of FixedSizeCollection a new instance of MutableCollection will be returned by withAll, and any variables that previously referenced the original collection will need to be redirected to reference the new instance. For other MutableCollection types you will replace the reference to collection with the same collection, since the instance will return "this" after calling addAll on itself.
      Specified by:
      withAll in interface MutableBag<T>
      Specified by:
      withAll in interface MutableBagIterable<T>
      Specified by:
      withAll in interface MutableCollection<T>
      See Also:
      Collection.addAll(Collection)
    • withoutAll

      public HashBag<T> withoutAll​(Iterable<? extends T> iterable)
      Description copied from interface: MutableCollection
      This method allows mutable and fixed size collections the ability to remove multiple elements from their existing elements. In order to support fixed size a new instance of a collection would have to be returned containing the elements that would be left from the original collection after calling removeAll. In the case of mutable collections, the original collection is modified, and is returned. In order to use this method properly with mutable and fixed size collections the following approach must be taken:
       MutableCollection<String> list = list.withoutAll(FastList.newListWith("1", "2"));
       
      In the case of FixedSizeCollection a new instance of MutableCollection will be returned by withoutAll, and any variables that previously referenced the original collection will need to be redirected to reference the new instance. For other MutableCollection types you will replace the reference to collection with the same collection, since the instance will return "this" after calling removeAll on itself.
      Specified by:
      withoutAll in interface MutableBag<T>
      Specified by:
      withoutAll in interface MutableBagIterable<T>
      Specified by:
      withoutAll in interface MutableCollection<T>
      See Also:
      Collection.removeAll(Collection)
    • with

      public HashBag<T> with​(T... elements)
    • with

      public HashBag<T> with​(T element1, T element2)
    • with

      public HashBag<T> with​(T element1, T element2, T element3)