All Implemented Interfaces:
Externalizable, Serializable, Cloneable, Comparable<SortedBag<T>>, Iterable<T>, Collection<T>, Bag<T>, MutableBagIterable<T>, MutableSortedBag<T>, SortedBag<T>, MutableCollection<T>, InternalIterable<T>, OrderedIterable<T>, ReversibleIterable<T>, SortedIterable<T>, RichIterable<T>

public class TreeBag<T> extends AbstractMutableSortedBag<T> implements Externalizable
A TreeBag is a MutableSortedBag which uses a SortedMap as its underlying data store. Each key in the SortedMap represents some item, and the value in the map represents the current number of occurrences of that item.
Since:
4.2
See Also:
  • Constructor Details

    • TreeBag

      public TreeBag()
    • TreeBag

      public TreeBag(Comparator<? super T> comparator)
    • TreeBag

      public TreeBag(SortedBag<T> sortedBag)
    • TreeBag

      public TreeBag(Comparator<? super T> comparator, Iterable<? extends T> iterable)
  • Method Details

    • newBag

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

      public static <E> TreeBag<E> newBag(Comparator<? super E> comparator)
    • newBag

      public static <E> TreeBag<E> newBag(Iterable<? extends E> source)
    • newBag

      public static <E> TreeBag<E> newBag(Comparator<? super E> comparator, Iterable<? extends E> iterable)
    • newBagWith

      public static <E> TreeBag<E> newBagWith(E... elements)
    • newBagWith

      public static <E> TreeBag<E> newBagWith(Comparator<? super E> comparator, E... elements)
    • clone

      public TreeBag<T> clone()
      Specified by:
      clone in interface MutableSortedBag<T>
      Specified by:
      clone in class AbstractMutableSortedBag<T>
    • 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:
    • 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:
    • sizeDistinct

      public int sizeDistinct()
      Description copied from interface: Bag
      The size of the Bag when counting only distinct elements.
      Specified by:
      sizeDistinct in interface Bag<T>
    • forEachWithOccurrences

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

      public MutableSortedBag<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 MutableBagIterable<T>
      Specified by:
      selectByOccurrences in interface MutableSortedBag<T>
      Specified by:
      selectByOccurrences in interface SortedBag<T>
    • occurrencesOf

      public int occurrencesOf(Object item)
      Description copied from interface: Bag
      The occurrences of a distinct item in the bag.
      Specified by:
      occurrencesOf in interface Bag<T>
    • anySatisfyWithOccurrences

      public boolean anySatisfyWithOccurrences(ObjectIntPredicate<? super T> predicate)
      Description copied from interface: Bag
      Returns true if the predicate evaluates to true for any element of the Bag. Returns false if the Bag is empty or if no element returns true for the predicate.
      Specified by:
      anySatisfyWithOccurrences in interface Bag<T>
    • allSatisfyWithOccurrences

      public boolean allSatisfyWithOccurrences(ObjectIntPredicate<? super T> predicate)
      Description copied from interface: Bag
      Returns true if the predicate evaluates to true for all elements of the Bag. Returns false if the Bag is empty or if not all elements return true for the predicate.
      Specified by:
      allSatisfyWithOccurrences in interface Bag<T>
    • noneSatisfyWithOccurrences

      public boolean noneSatisfyWithOccurrences(ObjectIntPredicate<? super T> predicate)
      Description copied from interface: Bag
      Returns true if the Bag is empty or if the predicate evaluates to false for all elements of the Bag. Returns false if the predicate evaluates to true for at least one element of the Bag.
      Specified by:
      noneSatisfyWithOccurrences in interface Bag<T>
    • detectWithOccurrences

      public T detectWithOccurrences(ObjectIntPredicate<? super T> predicate)
      Description copied from interface: Bag
      Returns an element of the Bag that satisfies the predicate or null if such an element does not exist
      Specified by:
      detectWithOccurrences in interface Bag<T>
    • 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>
    • remove

      public boolean remove(Object item)
      Specified by:
      remove in interface Collection<T>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<T>
    • 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>
    • compareTo

      public int compareTo(SortedBag<T> otherBag)
      Specified by:
      compareTo in interface Comparable<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
    • 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).
      Specified by:
      each in interface RichIterable<T>
      See Also:
    • 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>
      Specified by:
      forEachWithIndex in interface OrderedIterable<T>
      Overrides:
      forEachWithIndex in class AbstractRichIterable<T>
    • forEach

      public void forEach(int fromIndex, int toIndex, Procedure<? super T> procedure)
      Description copied from interface: OrderedIterable
      Iterates over the section of the iterable covered by the specified inclusive indexes. The indexes are both inclusive.
      e.g.
       OrderedIterable<People> people = FastList.newListWith(ted, mary, bob, sally)
       people.forEach(0, 1, new Procedure<Person>()
       {
           public void value(Person person)
           {
                LOGGER.info(person.getName());
           }
       });
       

      This code would output ted and mary's names.

      Specified by:
      forEach in interface OrderedIterable<T>
    • forEachWithIndex

      public void forEachWithIndex(int fromIndex, int toIndex, ObjectIntProcedure<? super T> objectIntProcedure)
      Description copied from interface: OrderedIterable
      Iterates over the section of the iterable covered by the specified inclusive indexes. The indexes are both inclusive.
      e.g.
       OrderedIterable<People> people = FastList.newListWith(ted, mary, bob, sally)
       people.forEachWithIndex(0, 1, new ObjectIntProcedure<Person>()
       {
           public void value(Person person, int index)
           {
                LOGGER.info(person.getName());
           }
       });
       

      This code would output ted and mary's names.

      Specified by:
      forEachWithIndex in interface OrderedIterable<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()
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>
    • 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));
       
      Specified by:
      addOccurrences in interface MutableBagIterable<T>
      Returns:
      updated number of occurrences.
    • removeOccurrences

      public boolean removeOccurrences(Object item, int occurrences)
      Specified by:
      removeOccurrences in interface MutableBagIterable<T>
    • setOccurrences

      public boolean setOccurrences(T item, int occurrences)
      Specified by:
      setOccurrences in interface MutableBagIterable<T>
    • without

      public TreeBag<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 MutableBagIterable<T>
      Specified by:
      without in interface MutableCollection<T>
      Specified by:
      without in interface MutableSortedBag<T>
      See Also:
    • withAll

      public TreeBag<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 MutableBagIterable<T>
      Specified by:
      withAll in interface MutableCollection<T>
      Specified by:
      withAll in interface MutableSortedBag<T>
      See Also:
    • withoutAll

      public TreeBag<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 MutableBagIterable<T>
      Specified by:
      withoutAll in interface MutableCollection<T>
      Specified by:
      withoutAll in interface MutableSortedBag<T>
      See Also:
    • with

      public TreeBag<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 MutableBagIterable<T>
      Specified by:
      with in interface MutableCollection<T>
      Specified by:
      with in interface MutableSortedBag<T>
      See Also:
    • newEmpty

      public MutableSortedBag<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 MutableCollection<T>
      Specified by:
      newEmpty in interface MutableSortedBag<T>
    • 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());
       
      Specified by:
      removeIf in interface MutableCollection<T>
    • 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);
       
      Specified by:
      removeIfWith in interface MutableCollection<T>
    • removeAllIterable

      public boolean removeAllIterable(Iterable<?> iterable)
      Specified by:
      removeAllIterable in interface MutableCollection<T>
      See Also:
    • size

      public int size()
      Description copied from interface: RichIterable
      Returns the number of items in this iterable.
      Specified by:
      size in interface Collection<T>
      Specified by:
      size in interface RichIterable<T>
    • indexOf

      public int indexOf(Object object)
      Description copied from interface: OrderedIterable
      Returns the index of the first occurrence of the specified item in this iterable, or -1 if this iterable does not contain the item.
      Specified by:
      indexOf in interface OrderedIterable<T>
      See Also:
    • zipWithIndex

      public MutableSortedSet<Pair<T,Integer>> zipWithIndex()
      Description copied from interface: RichIterable
      Zips this RichIterable with its indices.
      Specified by:
      zipWithIndex in interface Bag<T>
      Specified by:
      zipWithIndex in interface MutableBagIterable<T>
      Specified by:
      zipWithIndex in interface MutableCollection<T>
      Specified by:
      zipWithIndex in interface MutableSortedBag<T>
      Specified by:
      zipWithIndex in interface OrderedIterable<T>
      Specified by:
      zipWithIndex in interface ReversibleIterable<T>
      Specified by:
      zipWithIndex in interface RichIterable<T>
      Specified by:
      zipWithIndex in interface SortedBag<T>
      Specified by:
      zipWithIndex in interface SortedIterable<T>
      Returns:
      A new RichIterable containing pairs consisting of all elements of this RichIterable paired with their index. Indices start at 0.
      See Also:
    • distinct

      public MutableSortedSet<T> distinct()
      Description copied from interface: SortedIterable
      Returns a new SortedIterable containing the distinct elements in this iterable.

      Conceptually similar to RichIterable.toSet().RichIterable.toList() but retains the original order. If an element appears multiple times in this iterable, the first one will be copied into the result.

      Specified by:
      distinct in interface MutableSortedBag<T>
      Specified by:
      distinct in interface OrderedIterable<T>
      Specified by:
      distinct in interface ReversibleIterable<T>
      Specified by:
      distinct in interface SortedBag<T>
      Specified by:
      distinct in interface SortedIterable<T>
      Returns:
      SortedIterable of distinct elements
    • groupBy

      public <V> TreeBagMultimap<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();
               }
           });
       
      Specified by:
      groupBy in interface Bag<T>
      Specified by:
      groupBy in interface MutableBagIterable<T>
      Specified by:
      groupBy in interface MutableCollection<T>
      Specified by:
      groupBy in interface MutableSortedBag<T>
      Specified by:
      groupBy in interface OrderedIterable<T>
      Specified by:
      groupBy in interface ReversibleIterable<T>
      Specified by:
      groupBy in interface RichIterable<T>
      Specified by:
      groupBy in interface SortedBag<T>
      Specified by:
      groupBy in interface SortedIterable<T>
    • groupByEach

      public <V> TreeBagMultimap<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.
      Specified by:
      groupByEach in interface Bag<T>
      Specified by:
      groupByEach in interface MutableBagIterable<T>
      Specified by:
      groupByEach in interface MutableCollection<T>
      Specified by:
      groupByEach in interface MutableSortedBag<T>
      Specified by:
      groupByEach in interface OrderedIterable<T>
      Specified by:
      groupByEach in interface ReversibleIterable<T>
      Specified by:
      groupByEach in interface RichIterable<T>
      Specified by:
      groupByEach in interface SortedBag<T>
      Specified by:
      groupByEach in interface SortedIterable<T>
    • detectIndex

      public int detectIndex(Predicate<? super T> predicate)
      Description copied from interface: OrderedIterable
      Returns the index of the first element of the OrderedIterable for which the predicate evaluates to true. Returns -1 if no element evaluates true for the predicate.
      Specified by:
      detectIndex in interface OrderedIterable<T>
    • corresponds

      public <S> boolean corresponds(OrderedIterable<S> other, Predicate2<? super T,? super S> predicate)
      Description copied from interface: OrderedIterable
      Returns true if both OrderedIterables have the same length and predicate returns true for all corresponding elements e1 of this OrderedIterable and e2 of other. The predicate is evaluated for each element at the same position of each OrderedIterable in a forward iteration order. This is a short circuit pattern.
      Specified by:
      corresponds in interface OrderedIterable<T>
    • toStack

      public MutableStack<T> toStack()
      Description copied from interface: OrderedIterable
      Converts the OrderedIterable to a mutable MutableStack implementation.
      Specified by:
      toStack in interface OrderedIterable<T>
    • take

      public MutableSortedBag<T> take(int count)
      Description copied from interface: ReversibleIterable
      Returns the first count elements of the iterable or all the elements in the iterable if count is greater than the length of the iterable.
      Specified by:
      take in interface MutableSortedBag<T>
      Specified by:
      take in interface ReversibleIterable<T>
      Specified by:
      take in interface SortedBag<T>
      Parameters:
      count - the number of items to take.
    • drop

      public MutableSortedBag<T> drop(int count)
      Description copied from interface: ReversibleIterable
      Returns an iterable after skipping the first count elements or an empty iterable if the count is greater than the length of the iterable.
      Specified by:
      drop in interface MutableSortedBag<T>
      Specified by:
      drop in interface ReversibleIterable<T>
      Specified by:
      drop in interface SortedBag<T>
      Parameters:
      count - the number of items to drop.
    • comparator

      public Comparator<? super T> comparator()
      Description copied from interface: SortedBag
      Returns the comparator used to order the elements in this bag, or null if this bag uses the natural ordering of its elements.
      Specified by:
      comparator in interface SortedBag<T>
      Specified by:
      comparator in interface SortedIterable<T>
    • with

      public TreeBag<T> with(T... elements)
    • with

      public TreeBag<T> with(T element1, T element2)
    • add

      public boolean add(T item)
      Specified by:
      add in interface Collection<T>
    • with

      public TreeBag<T> with(T element1, T element2, T element3)