Class AbstractHashBag<T>

All Implemented Interfaces:
Iterable<T>, Collection<T>, Bag<T>, MutableBag<T>, MutableBagIterable<T>, UnsortedBag<T>, MutableCollection<T>, InternalIterable<T>, RichIterable<T>
Direct Known Subclasses:
HashBag, HashBagWithHashingStrategy

public abstract class AbstractHashBag<T>
extends AbstractMutableBag<T>
  • Constructor Details

  • Method Details

    • addOccurrences

      public int addOccurrences​(T item, int occurrences)
      Description copied from interface: MutableBagIterable
      Add number of occurrences for an item. If the item does not exist, then the item is added to the bag.

      For Example:

       MutableBagIterable<String> names = Bags.mutable.of("A", "B", "B");
       Assert.assertEquals(4, names.addOccurrences("A", 3));
       
      Returns:
      updated number of occurrences.
    • equals

      public boolean equals​(Object other)
      Description copied from interface: Bag
      Two bags b1 and b2 are equal if m1.toMapOfItemToCount().equals(m2.toMapOfItemToCount()).
      Specified by:
      equals in interface Bag<T>
      Specified by:
      equals in interface Collection<T>
      Overrides:
      equals in class Object
      See Also:
      Map.equals(Object)
    • hashCode

      public int hashCode()
      Description copied from interface: Bag
      Returns the hash code for this Bag, defined as this.Bag.toMapOfItemToCount().hashCode().
      Specified by:
      hashCode in interface Bag<T>
      Specified by:
      hashCode in interface Collection<T>
      Overrides:
      hashCode in class Object
      See Also:
      Map.hashCode()
    • sizeDistinct

      public int sizeDistinct()
      Description copied from interface: Bag
      The size of the Bag when counting only distinct elements.
    • occurrencesOf

      public int occurrencesOf​(Object item)
      Description copied from interface: Bag
      The occurrences of a distinct item in the bag.
    • forEachWithOccurrences

      public void forEachWithOccurrences​(ObjectIntProcedure<? super T> objectIntProcedure)
      Description copied from interface: Bag
      For each distinct item, with the number of occurrences, execute the specified procedure.
    • toMapOfItemToCount

      public MutableMap<T,​Integer> toMapOfItemToCount()
      Description copied from interface: Bag
      Converts the Bag to a Map of the Item type to its count as an Integer.
    • add

      public boolean add​(T item)
    • remove

      public boolean remove​(Object item)
    • clear

      public void clear()
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: RichIterable
      Returns true if this iterable has zero items.
      Specified by:
      isEmpty in interface Collection<T>
      Specified by:
      isEmpty in interface RichIterable<T>
      Overrides:
      isEmpty in class AbstractRichIterable<T>
    • each

      public void each​(Procedure<? super T> procedure)
      Description copied from interface: RichIterable
      The procedure is executed for each element in the iterable.

      Example using a Java 8 lambda expression:

       people.each(person -> LOGGER.info(person.getName()));
       

      Example using an anonymous inner class:

       people.each(new Procedure<Person>()
       {
           public void value(Person person)
           {
               LOGGER.info(person.getName());
           }
       });
       
      This method is a variant of InternalIterable.forEach(Procedure) that has a signature conflict with Iterable.forEach(java.util.function.Consumer).
      See Also:
      InternalIterable.forEach(Procedure), Iterable.forEach(java.util.function.Consumer)
    • forEachWithIndex

      public void forEachWithIndex​(ObjectIntProcedure<? super T> objectIntProcedure)
      Description copied from interface: InternalIterable
      Iterates over the iterable passing each element and the current relative int index to the specified instance of ObjectIntProcedure.

      Example using a Java 8 lambda:

       people.forEachWithIndex((Person person, int index) -> LOGGER.info("Index: " + index + " person: " + person.getName()));
       

      Example using an anonymous inner class:

       people.forEachWithIndex(new ObjectIntProcedure<Person>()
       {
           public void value(Person person, int index)
           {
               LOGGER.info("Index: " + index + " person: " + person.getName());
           }
       });
       
      Specified by:
      forEachWithIndex in interface InternalIterable<T>
      Overrides:
      forEachWithIndex in class AbstractRichIterable<T>
    • forEachWith

      public <P> void forEachWith​(Procedure2<? super T,​? super P> procedure, P parameter)
      Description copied from interface: InternalIterable
      The procedure2 is evaluated for each element in the iterable with the specified parameter provided as the second argument.

      Example using a Java 8 lambda:

       people.forEachWith((Person person, Person other) ->
           {
               if (person.isRelatedTo(other))
               {
                    LOGGER.info(person.getName());
               }
           }, fred);
       

      Example using an anonymous inner class:

       people.forEachWith(new Procedure2<Person, Person>()
       {
           public void value(Person person, Person other)
           {
               if (person.isRelatedTo(other))
               {
                    LOGGER.info(person.getName());
               }
           }
       }, fred);
       
      Specified by:
      forEachWith in interface InternalIterable<T>
      Overrides:
      forEachWith in class AbstractRichIterable<T>
    • iterator

      public Iterator<T> iterator()
    • removeOccurrences

      public boolean removeOccurrences​(Object item, int occurrences)
    • setOccurrences

      public boolean setOccurrences​(T item, int occurrences)
    • removeIf

      public boolean removeIf​(Predicate<? super T> predicate)
      Description copied from interface: MutableCollection
      Removes all elements in the collection that evaluate to true for the specified predicate.
      e.g.
       return lastNames.removeIf(Predicates.isNull());
       
    • removeIfWith

      public <P> boolean removeIfWith​(Predicate2<? super T,​? super P> predicate, P parameter)
      Description copied from interface: MutableCollection
      Removes all elements in the collection that evaluate to true for the specified predicate2 and parameter.
       return lastNames.removeIfWith(Predicates2.isNull(), null);
       
    • removeAllIterable

      public boolean removeAllIterable​(Iterable<?> iterable)
      See Also:
      Collection.removeAll(Collection)
    • size

      public int size()
      Description copied from interface: RichIterable
      Returns the number of items in this iterable.
    • contains

      public boolean contains​(Object o)
      Description copied from interface: RichIterable
      Returns true if the iterable has an element which responds true to element.equals(object).
      Specified by:
      contains in interface Collection<T>
      Specified by:
      contains in interface RichIterable<T>
      Overrides:
      contains in class AbstractRichIterable<T>
    • groupBy

      public <V> HashBagMultimap<V,​T> groupBy​(Function<? super T,​? extends V> function)
      Description copied from interface: RichIterable
      For each element of the iterable, the function is evaluated and the results of these evaluations are collected into a new multimap, where the transformed value is the key and the original values are added to the same (or similar) species of collection as the source iterable.

      Example using a Java 8 method reference:

       Multimap<String, Person> peopleByLastName =
           people.groupBy(Person::getLastName);
       

      Example using an anonymous inner class:

       Multimap<String, Person> peopleByLastName =
           people.groupBy(new Function<Person, String>()
           {
               public String valueOf(Person person)
               {
                   return person.getLastName();
               }
           });
       
    • groupByEach

      public <V> HashBagMultimap<V,​T> groupByEach​(Function<? super T,​? extends Iterable<V>> function)
      Description copied from interface: RichIterable
      Similar to RichIterable.groupBy(Function), except the result of evaluating function will return a collection of keys for each value.