Class ImmutableHashBag<T>

All Implemented Interfaces:
Serializable, Iterable<T>, Collection<T>, Bag<T>, ImmutableBag<T>, ImmutableBagIterable<T>, UnsortedBag<T>, ImmutableCollection<T>, InternalIterable<T>, RichIterable<T>

public class ImmutableHashBag<T> extends AbstractImmutableBag<T> implements Serializable
Since:
1.0
See Also:
  • Constructor Details

    • ImmutableHashBag

      public ImmutableHashBag()
    • ImmutableHashBag

      public ImmutableHashBag(Iterable<? extends T> source)
    • ImmutableHashBag

      public ImmutableHashBag(Bag<? extends T> source)
  • Method Details

    • newBag

      public static <T> ImmutableHashBag<T> newBag()
    • newBag

      public static <T> ImmutableHashBag<T> newBag(Iterable<? extends T> source)
    • newBagWith

      public static <T> ImmutableHashBag<T> newBagWith(T... elements)
    • newBagWith

      public static <T> ImmutableHashBag<T> newBagWith(Bag<? extends T> bag)
    • newWith

      public ImmutableBag<T> newWith(T element)
      Description copied from interface: ImmutableCollection
      This method is similar to the with method in MutableCollection with the difference that a new copy of this collection with the element appended will be returned.
      Specified by:
      newWith in interface ImmutableBag<T>
      Specified by:
      newWith in interface ImmutableCollection<T>
    • newWithout

      public ImmutableBag<T> newWithout(T element)
      Description copied from interface: ImmutableCollection
      This method is similar to the without method in MutableCollection with the difference that a new copy of this collection with the element removed will be returned.
      Specified by:
      newWithout in interface ImmutableBag<T>
      Specified by:
      newWithout in interface ImmutableCollection<T>
    • newWithAll

      public ImmutableBag<T> newWithAll(Iterable<? extends T> elements)
      Description copied from interface: ImmutableCollection
      This method is similar to the withAll method in MutableCollection with the difference that a new copy of this collection with the elements appended will be returned.
      Specified by:
      newWithAll in interface ImmutableBag<T>
      Specified by:
      newWithAll in interface ImmutableCollection<T>
    • 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>
    • groupBy

      public <V> ImmutableBagMultimap<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 ImmutableBag<T>
      Specified by:
      groupBy in interface ImmutableBagIterable<T>
      Specified by:
      groupBy in interface ImmutableCollection<T>
      Specified by:
      groupBy in interface RichIterable<T>
      Specified by:
      groupBy in interface UnsortedBag<T>
    • groupBy

      public <V, R extends MutableMultimap<V, T>> R groupBy(Function<? super T,? extends V> function, R target)
      Description copied from interface: RichIterable
      Same as RichIterable.groupBy(Function), except that the results are gathered into the specified target multimap.

      Example using a Java 8 method reference:

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

      Example using an anonymous inner class:

       FastListMultimap<String, Person> peopleByLastName =
           people.groupBy(new Function<Person, String>()
           {
               public String valueOf(Person person)
               {
                   return person.getLastName();
               }
           }, new FastListMultimap<String, Person>());
       
      Specified by:
      groupBy in interface RichIterable<T>
      Overrides:
      groupBy in class AbstractBag<T>
    • groupByEach

      public <V> ImmutableBagMultimap<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 ImmutableBag<T>
      Specified by:
      groupByEach in interface ImmutableBagIterable<T>
      Specified by:
      groupByEach in interface ImmutableCollection<T>
      Specified by:
      groupByEach in interface RichIterable<T>
      Specified by:
      groupByEach in interface UnsortedBag<T>
    • groupByEach

      public <V, R extends MutableMultimap<V, T>> R groupByEach(Function<? super T,? extends Iterable<V>> function, R target)
      Description copied from interface: RichIterable
      Same as RichIterable.groupByEach(Function), except that the results are gathered into the specified target multimap.
      Specified by:
      groupByEach in interface RichIterable<T>
      Overrides:
      groupByEach in class AbstractBag<T>
    • groupByUniqueKey

      public <V> ImmutableMap<V,T> groupByUniqueKey(Function<? super T,? extends V> function)
      Description copied from interface: RichIterable
      For each element of the iterable, the function is evaluated and he results of these evaluations are collected into a new map, where the transformed value is the key. The generated keys must each be unique, or else an exception is thrown.
      Specified by:
      groupByUniqueKey in interface ImmutableCollection<T>
      Specified by:
      groupByUniqueKey in interface RichIterable<T>
      Overrides:
      groupByUniqueKey in class AbstractImmutableBag<T>
      See Also:
    • 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>
    • notEmpty

      public boolean notEmpty()
      Description copied from interface: RichIterable
      The English equivalent of !this.isEmpty()
      Specified by:
      notEmpty in interface RichIterable<T>
    • getFirst

      public T getFirst()
      Description copied from interface: RichIterable
      Returns the first element of an iterable. In the case of a List it is the element at the first index. In the case of any other Collection, it is the first element that would be returned during an iteration. If the iterable is empty, null is returned. If null is a valid element of the container, then a developer would need to check to see if the iterable is empty to validate that a null result was not due to the container being empty.

      The order of Sets are not guaranteed (except for TreeSets and other Ordered Set implementations), so if you use this method, the first element could be any element from the Set.

      Specified by:
      getFirst in interface RichIterable<T>
    • getLast

      public T getLast()
      Description copied from interface: RichIterable
      Returns the last element of an iterable. In the case of a List it is the element at the last index. In the case of any other Collection, it is the last element that would be returned during an iteration. If the iterable is empty, null is returned. If null is a valid element of the container, then a developer would need to check to see if the iterable is empty to validate that a null result was not due to the container being empty.

      The order of Sets are not guaranteed (except for TreeSets and other Ordered Set implementations), so if you use this method, the last element could be any element from the Set.

      Specified by:
      getLast in interface RichIterable<T>
    • getOnly

      public T getOnly()
      Description copied from interface: RichIterable
      Returns the element if the iterable has exactly one element. Otherwise, throw IllegalStateException.
      Specified by:
      getOnly in interface RichIterable<T>
      Returns:
      an element of an iterable.
    • min

      public T min(Comparator<? super T> comparator)
      Description copied from interface: RichIterable
      Returns the minimum element out of this container based on the comparator.
      Specified by:
      min in interface RichIterable<T>
      Overrides:
      min in class AbstractRichIterable<T>
    • max

      public T max(Comparator<? super T> comparator)
      Description copied from interface: RichIterable
      Returns the maximum element out of this container based on the comparator.
      Specified by:
      max in interface RichIterable<T>
      Overrides:
      max in class AbstractRichIterable<T>
    • min

      public T min()
      Description copied from interface: RichIterable
      Returns the minimum element out of this container based on the natural order.
      Specified by:
      min in interface RichIterable<T>
      Overrides:
      min in class AbstractRichIterable<T>
    • max

      public T max()
      Description copied from interface: RichIterable
      Returns the maximum element out of this container based on the natural order.
      Specified by:
      max in interface RichIterable<T>
      Overrides:
      max in class AbstractRichIterable<T>
    • minBy

      public <V extends Comparable<? super V>> T minBy(Function<? super T,? extends V> function)
      Description copied from interface: RichIterable
      Returns the minimum elements out of this container based on the natural order of the attribute returned by Function.
      Specified by:
      minBy in interface RichIterable<T>
      Overrides:
      minBy in class AbstractRichIterable<T>
    • maxBy

      public <V extends Comparable<? super V>> T maxBy(Function<? super T,? extends V> function)
      Description copied from interface: RichIterable
      Returns the maximum elements out of this container based on the natural order of the attribute returned by Function.
      Specified by:
      maxBy in interface RichIterable<T>
      Overrides:
      maxBy in class AbstractRichIterable<T>
    • contains

      public boolean contains(Object object)
      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>
    • containsAllIterable

      public boolean containsAllIterable(Iterable<?> source)
      Description copied from interface: RichIterable
      Returns true if all elements in source are contained in this collection.
      Specified by:
      containsAllIterable in interface RichIterable<T>
      Overrides:
      containsAllIterable in class AbstractRichIterable<T>
    • containsAllArguments

      public boolean containsAllArguments(Object... elements)
      Description copied from interface: RichIterable
      Returns true if all elements in the specified var arg array are contained in this collection.
      Specified by:
      containsAllArguments in interface RichIterable<T>
      Overrides:
      containsAllArguments in class AbstractRichIterable<T>
    • toMap

      public <K, V> MutableMap<K,V> toMap(Function<? super T,? extends K> keyFunction, Function<? super T,? extends V> valueFunction)
      Description copied from interface: RichIterable
      Converts the collection to a MutableMap implementation using the specified key and value functions.
      Specified by:
      toMap in interface RichIterable<T>
      Overrides:
      toMap in class AbstractRichIterable<T>
    • toMap

      public <K, V, R extends Map<K, V>> R toMap(Function<? super T,? extends K> keyFunction, Function<? super T,? extends V> valueFunction, R target)
      Description copied from interface: RichIterable
      Same as RichIterable.toMap(Function, Function), except that the results are gathered into the specified target map.
      Specified by:
      toMap in interface RichIterable<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).
      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>
      Overrides:
      forEachWithIndex in class AbstractRichIterable<T>
    • selectInstancesOf

      public <S> ImmutableBag<S> selectInstancesOf(Class<S> clazz)
      Description copied from interface: RichIterable
      Returns all elements of the source collection that are instances of the Class clazz.
       RichIterable<Integer> integers =
           List.mutable.with(new Integer(0), new Long(0L), new Double(0.0)).selectInstancesOf(Integer.class);
       
      Specified by:
      selectInstancesOf in interface Bag<T>
      Specified by:
      selectInstancesOf in interface ImmutableBag<T>
      Specified by:
      selectInstancesOf in interface ImmutableBagIterable<T>
      Specified by:
      selectInstancesOf in interface ImmutableCollection<T>
      Specified by:
      selectInstancesOf in interface RichIterable<T>
      Specified by:
      selectInstancesOf in interface UnsortedBag<T>
    • selectByOccurrences

      public ImmutableBag<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 ImmutableBag<T>
      Specified by:
      selectByOccurrences in interface ImmutableBagIterable<T>
      Specified by:
      selectByOccurrences in interface UnsortedBag<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>
    • 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.
      Specified by:
      forEachWithOccurrences in interface Bag<T>
    • 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>
    • 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>
    • toList

      public MutableList<T> toList()
      Description copied from interface: RichIterable
      Converts the collection to a MutableList implementation.
      Specified by:
      toList in interface RichIterable<T>
      Overrides:
      toList in class AbstractBag<T>
    • toSortedList

      public MutableList<T> toSortedList()
      Description copied from interface: RichIterable
      Converts the collection to a MutableList implementation and sorts it using the natural order of the elements.
      Specified by:
      toSortedList in interface RichIterable<T>
    • toSortedList

      public MutableList<T> toSortedList(Comparator<? super T> comparator)
      Description copied from interface: RichIterable
      Converts the collection to a MutableList implementation and sorts it using the specified comparator.
      Specified by:
      toSortedList in interface RichIterable<T>
      Overrides:
      toSortedList in class AbstractBag<T>
    • toSortedSet

      public MutableSortedSet<T> toSortedSet()
      Description copied from interface: RichIterable
      Converts the collection to a MutableSortedSet implementation and sorts it using the natural order of the elements.
      Specified by:
      toSortedSet in interface RichIterable<T>
      Overrides:
      toSortedSet in class AbstractBag<T>
    • toSortedSet

      public MutableSortedSet<T> toSortedSet(Comparator<? super T> comparator)
      Description copied from interface: RichIterable
      Converts the collection to a MutableSortedSet implementation and sorts it using the specified comparator.
      Specified by:
      toSortedSet in interface RichIterable<T>
      Overrides:
      toSortedSet in class AbstractBag<T>
    • toSortedMap

      public <K, V> MutableSortedMap<K,V> toSortedMap(Function<? super T,? extends K> keyFunction, Function<? super T,? extends V> valueFunction)
      Description copied from interface: RichIterable
      Converts the collection to a MutableSortedMap implementation using the specified key and value functions sorted by the key elements' natural ordering.
      Specified by:
      toSortedMap in interface RichIterable<T>
      Overrides:
      toSortedMap in class AbstractRichIterable<T>
    • toSortedMap

      public <K, V> MutableSortedMap<K,V> toSortedMap(Comparator<? super K> comparator, Function<? super T,? extends K> keyFunction, Function<? super T,? extends V> valueFunction)
      Description copied from interface: RichIterable
      Converts the collection to a MutableSortedMap implementation using the specified key and value functions sorted by the given comparator.
      Specified by:
      toSortedMap in interface RichIterable<T>
      Overrides:
      toSortedMap in class AbstractRichIterable<T>
    • toSortedMapBy

      public <KK extends Comparable<? super KK>, K, V> MutableSortedMap<K,V> toSortedMapBy(Function<? super K,KK> sortBy, Function<? super T,? extends K> keyFunction, Function<? super T,? extends V> valueFunction)
      Description copied from interface: RichIterable
      Converts the collection to a MutableSortedMap implementation using the specified key and value functions and sorts it based on the natural order of the attribute returned by sortBy function.
      Specified by:
      toSortedMapBy in interface RichIterable<T>
      Overrides:
      toSortedMapBy in class AbstractRichIterable<T>
    • select

      public ImmutableBag<T> select(Predicate<? super T> predicate)
      Description copied from interface: RichIterable
      Returns all elements of the source collection that return true when evaluating the predicate. This method is also commonly called filter.

      Example using a Java 8 lambda expression:

       RichIterable<Person> selected =
           people.select(person -> person.getAddress().getCity().equals("London"));
       

      Example using an anonymous inner class:

       RichIterable<Person> selected =
           people.select(new Predicate<Person>()
           {
               public boolean accept(Person person)
               {
                   return person.getAddress().getCity().equals("London");
               }
           });
       
      Specified by:
      select in interface Bag<T>
      Specified by:
      select in interface ImmutableBag<T>
      Specified by:
      select in interface ImmutableBagIterable<T>
      Specified by:
      select in interface ImmutableCollection<T>
      Specified by:
      select in interface RichIterable<T>
      Specified by:
      select in interface UnsortedBag<T>
    • select

      public <R extends Collection<T>> R select(Predicate<? super T> predicate, R target)
      Description copied from interface: RichIterable
      Same as the select method with one parameter but uses the specified target collection for the results.

      Example using a Java 8 lambda expression:

       MutableList<Person> selected =
           people.select(person -> person.person.getLastName().equals("Smith"), Lists.mutable.empty());
       

      Example using an anonymous inner class:

       MutableList<Person> selected =
           people.select(new Predicate<Person>()
           {
               public boolean accept(Person person)
               {
                   return person.person.getLastName().equals("Smith");
               }
           }, Lists.mutable.empty());
       

      Specified by:
      select in interface RichIterable<T>
      Overrides:
      select in class AbstractBag<T>
      Parameters:
      predicate - a Predicate to use as the select criteria
      target - the Collection to append to for all elements in this RichIterable that meet select criteria predicate
      Returns:
      target, which contains appended elements as a result of the select criteria
      See Also:
    • reject

      public ImmutableBag<T> reject(Predicate<? super T> predicate)
      Description copied from interface: RichIterable
      Returns all elements of the source collection that return false when evaluating of the predicate. This method is also sometimes called filterNot and is the equivalent of calling iterable.select(Predicates.not(predicate)).

      Example using a Java 8 lambda expression:

       RichIterable<Person> rejected =
           people.reject(person -> person.person.getLastName().equals("Smith"));
       

      Example using an anonymous inner class:

       RichIterable<Person> rejected =
           people.reject(new Predicate<Person>()
           {
               public boolean accept(Person person)
               {
                   return person.person.getLastName().equals("Smith");
               }
           });
       
      Specified by:
      reject in interface Bag<T>
      Specified by:
      reject in interface ImmutableBag<T>
      Specified by:
      reject in interface ImmutableBagIterable<T>
      Specified by:
      reject in interface ImmutableCollection<T>
      Specified by:
      reject in interface RichIterable<T>
      Specified by:
      reject in interface UnsortedBag<T>
      Parameters:
      predicate - a Predicate to use as the reject criteria
      Returns:
      a RichIterable that contains elements that cause Predicate.accept(Object) method to evaluate to false
    • reject

      public <R extends Collection<T>> R reject(Predicate<? super T> predicate, R target)
      Description copied from interface: RichIterable
      Same as the reject method with one parameter but uses the specified target collection for the results.

      Example using a Java 8 lambda expression:

       MutableList<Person> rejected =
           people.reject(person -> person.person.getLastName().equals("Smith"), Lists.mutable.empty());
       

      Example using an anonymous inner class:

       MutableList<Person> rejected =
           people.reject(new Predicate<Person>()
           {
               public boolean accept(Person person)
               {
                   return person.person.getLastName().equals("Smith");
               }
           }, Lists.mutable.empty());
       
      Specified by:
      reject in interface RichIterable<T>
      Overrides:
      reject in class AbstractBag<T>
      Parameters:
      predicate - a Predicate to use as the reject criteria
      target - the Collection to append to for all elements in this RichIterable that cause Predicate#accept(Object) method to evaluate to false
      Returns:
      target, which contains appended elements as a result of the reject criteria
    • partition

      public PartitionImmutableBag<T> partition(Predicate<? super T> predicate)
      Description copied from interface: RichIterable
      Filters a collection into a PartitionedIterable based on the evaluation of the predicate.

      Example using a Java 8 lambda expression:

       PartitionIterable<Person> newYorkersAndNonNewYorkers =
           people.partition(person -> person.getAddress().getState().getName().equals("New York"));
       

      Example using an anonymous inner class:

       PartitionIterable<Person> newYorkersAndNonNewYorkers =
           people.partition(new Predicate<Person>()
           {
               public boolean accept(Person person)
               {
                   return person.getAddress().getState().getName().equals("New York");
               }
           });
       
      Specified by:
      partition in interface Bag<T>
      Specified by:
      partition in interface ImmutableBag<T>
      Specified by:
      partition in interface ImmutableBagIterable<T>
      Specified by:
      partition in interface ImmutableCollection<T>
      Specified by:
      partition in interface RichIterable<T>
      Specified by:
      partition in interface UnsortedBag<T>
      Overrides:
      partition in class AbstractImmutableBag<T>
    • collect

      public <V> ImmutableBag<V> collect(Function<? super T,? extends V> function)
      Description copied from interface: RichIterable
      Returns a new collection with the results of applying the specified function on each element of the source collection. This method is also commonly called transform or map.

      Example using a Java 8 lambda expression:

       RichIterable<String> names =
           people.collect(person -> person.getFirstName() + " " + person.getLastName());
       

      Example using an anonymous inner class:

       RichIterable<String> names =
           people.collect(new Function<Person, String>()
           {
               public String valueOf(Person person)
               {
                   return person.getFirstName() + " " + person.getLastName();
               }
           });
       
      Specified by:
      collect in interface ImmutableBag<T>
      Specified by:
      collect in interface ImmutableCollection<T>
      Specified by:
      collect in interface RichIterable<T>
      Specified by:
      collect in interface UnsortedBag<T>
    • collect

      public <V, R extends Collection<V>> R collect(Function<? super T,? extends V> function, R target)
      Description copied from interface: RichIterable
      Same as RichIterable.collect(Function), except that the results are gathered into the specified target collection.

      Example using a Java 8 lambda expression:

       MutableList<String> names =
           people.collect(person -> person.getFirstName() + " " + person.getLastName(), Lists.mutable.empty());
       

      Example using an anonymous inner class:

       MutableList<String> names =
           people.collect(new Function<Person, String>()
           {
               public String valueOf(Person person)
               {
                   return person.getFirstName() + " " + person.getLastName();
               }
           }, Lists.mutable.empty());
       
      Specified by:
      collect in interface RichIterable<T>
      Overrides:
      collect in class AbstractBag<T>
      Parameters:
      function - a Function to use as the collect transformation function
      target - the Collection to append to for all elements in this RichIterable that meet select criteria function
      Returns:
      target, which contains appended elements as a result of the collect transformation
      See Also:
    • collectIf

      public <V> ImmutableBag<V> collectIf(Predicate<? super T> predicate, Function<? super T,? extends V> function)
      Description copied from interface: RichIterable
      Returns a new collection with the results of applying the specified function on each element of the source collection, but only for those elements which return true upon evaluation of the predicate. This is the the optimized equivalent of calling iterable.select(predicate).collect(function).

      Example using a Java 8 lambda and method reference:

       RichIterable<String> strings = Lists.mutable.with(1, 2, 3).collectIf(e -> e != null, Object::toString);
       

      Example using Predicates factory:

       RichIterable<String> strings = Lists.mutable.with(1, 2, 3).collectIf(Predicates.notNull(), Functions.getToString());
       
      Specified by:
      collectIf in interface ImmutableBag<T>
      Specified by:
      collectIf in interface ImmutableCollection<T>
      Specified by:
      collectIf in interface RichIterable<T>
      Specified by:
      collectIf in interface UnsortedBag<T>
    • collectIf

      public <V, R extends Collection<V>> R collectIf(Predicate<? super T> predicate, Function<? super T,? extends V> function, R target)
      Description copied from interface: RichIterable
      Same as the collectIf method with two parameters but uses the specified target collection for the results.
      Specified by:
      collectIf in interface RichIterable<T>
      Overrides:
      collectIf in class AbstractBag<T>
      Parameters:
      predicate - a Predicate to use as the select criteria
      function - a Function to use as the collect transformation function
      target - the Collection to append to for all elements in this RichIterable that meet the collect criteria predicate
      Returns:
      targetCollection, which contains appended elements as a result of the collect criteria and transformation
      See Also:
    • flatCollect

      public <V> ImmutableBag<V> flatCollect(Function<? super T,? extends Iterable<V>> function)
      Description copied from interface: RichIterable
      flatCollect is a special case of RichIterable.collect(Function). With collect, when the Function returns a collection, the result is a collection of collections. flatCollect outputs a single "flattened" collection instead. This method is commonly called flatMap.

      Consider the following example where we have a Person class, and each Person has a list of Address objects. Take the following Function:

       Function<Person, List<Address>> addressFunction = Person::getAddresses;
       RichIterable<Person> people = ...;
       
      Using collect returns a collection of collections of addresses.
       RichIterable<List<Address>> addresses = people.collect(addressFunction);
       
      Using flatCollect returns a single flattened list of addresses.
       RichIterable<Address> addresses = people.flatCollect(addressFunction);
       
      Specified by:
      flatCollect in interface ImmutableBag<T>
      Specified by:
      flatCollect in interface ImmutableCollection<T>
      Specified by:
      flatCollect in interface RichIterable<T>
      Specified by:
      flatCollect in interface UnsortedBag<T>
      Parameters:
      function - The Function to apply
      Returns:
      a new flattened collection produced by applying the given function
    • flatCollect

      public <V, R extends Collection<V>> R flatCollect(Function<? super T,? extends Iterable<V>> function, R target)
      Description copied from interface: RichIterable
      Same as flatCollect, only the results are collected into the target collection.
      Specified by:
      flatCollect in interface RichIterable<T>
      Overrides:
      flatCollect in class AbstractBag<T>
      Parameters:
      function - The Function to apply
      target - The collection into which results should be added.
      Returns:
      target, which will contain a flattened collection of results produced by applying the given function
      See Also:
    • detect

      public T detect(Predicate<? super T> predicate)
      Description copied from interface: RichIterable
      Returns the first element of the iterable for which the predicate evaluates to true or null in the case where no element returns true. This method is commonly called find.

      Example using a Java 8 lambda expression:

       Person person =
           people.detect(person -> person.getFirstName().equals("John") && person.getLastName().equals("Smith"));
       

      Example using an anonymous inner class:

       Person person =
           people.detect(new Predicate<Person>()
           {
               public boolean accept(Person person)
               {
                   return person.getFirstName().equals("John") && person.getLastName().equals("Smith");
               }
           });
       
      Specified by:
      detect in interface RichIterable<T>
      Overrides:
      detect in class AbstractRichIterable<T>
    • detectWith

      public <P> T detectWith(Predicate2<? super T,? super P> predicate, P parameter)
      Description copied from interface: RichIterable
      Returns the first element that evaluates to true for the specified predicate2 and parameter, or null if none evaluate to true.

      Example using a Java 8 lambda expression:

       Person person =
           people.detectWith((person, fullName) -> person.getFullName().equals(fullName), "John Smith");
       

      Example using an anonymous inner class:

       Person person =
           people.detectWith(new Predicate2<Person, String>()
           {
               public boolean accept(Person person, String fullName)
               {
                   return person.getFullName().equals(fullName);
               }
           }, "John Smith");
       
      Specified by:
      detectWith in interface RichIterable<T>
      Overrides:
      detectWith in class AbstractRichIterable<T>
    • detectOptional

      public Optional<T> detectOptional(Predicate<? super T> predicate)
      Description copied from interface: RichIterable
      Returns the first element of the iterable for which the predicate evaluates to true as an Optional. This method is commonly called find.

      Example using a Java 8 lambda expression:

       Person person =
           people.detectOptional(person -> person.getFirstName().equals("John") && person.getLastName().equals("Smith"));
       

      Specified by:
      detectOptional in interface RichIterable<T>
      Overrides:
      detectOptional in class AbstractRichIterable<T>
    • detectWithOptional

      public <P> Optional<T> detectWithOptional(Predicate2<? super T,? super P> predicate, P parameter)
      Description copied from interface: RichIterable
      Returns the first element that evaluates to true for the specified predicate2 and parameter as an Optional.

      Example using a Java 8 lambda expression:

       Optional<Person> person =
           people.detectWithOptional((person, fullName) -> person.getFullName().equals(fullName), "John Smith");
       

      Specified by:
      detectWithOptional in interface RichIterable<T>
      Overrides:
      detectWithOptional in class AbstractRichIterable<T>
    • detectIfNone

      public T detectIfNone(Predicate<? super T> predicate, Function0<? extends T> function)
      Description copied from interface: RichIterable
      Returns the first element of the iterable for which the predicate evaluates to true. If no element matches the predicate, then returns the value of applying the specified function.
      Specified by:
      detectIfNone in interface RichIterable<T>
    • count

      public int count(Predicate<? super T> predicate)
      Description copied from interface: RichIterable
      Return the total number of elements that answer true to the specified predicate.

      Example using a Java 8 lambda expression:

       int count =
           people.count(person -> person.getAddress().getState().getName().equals("New York"));
       

      Example using an anonymous inner class:

       int count =
           people.count(new Predicate<Person>()
           {
               public boolean accept(Person person)
               {
                   return person.getAddress().getState().getName().equals("New York");
               }
           });
       
      Specified by:
      count in interface RichIterable<T>
      Overrides:
      count in class AbstractBag<T>
    • anySatisfy

      public boolean anySatisfy(Predicate<? super T> predicate)
      Description copied from interface: RichIterable
      Returns true if the predicate evaluates to true for any element of the iterable. Returns false if the iterable is empty, or if no element returned true when evaluating the predicate.
      Specified by:
      anySatisfy in interface RichIterable<T>
      Overrides:
      anySatisfy in class AbstractRichIterable<T>
    • anySatisfyWith

      public <P> boolean anySatisfyWith(Predicate2<? super T,? super P> predicate, P parameter)
      Description copied from interface: RichIterable
      Returns true if the predicate evaluates to true for any element of the collection, or return false. Returns false if the collection is empty.
      Specified by:
      anySatisfyWith in interface RichIterable<T>
      Overrides:
      anySatisfyWith in class AbstractRichIterable<T>
    • allSatisfy

      public boolean allSatisfy(Predicate<? super T> predicate)
      Description copied from interface: RichIterable
      Returns true if the predicate evaluates to true for every element of the iterable or if the iterable is empty. Otherwise, returns false.
      Specified by:
      allSatisfy in interface RichIterable<T>
      Overrides:
      allSatisfy in class AbstractRichIterable<T>
    • allSatisfyWith

      public <P> boolean allSatisfyWith(Predicate2<? super T,? super P> predicate, P parameter)
      Description copied from interface: RichIterable
      Returns true if the predicate evaluates to true for every element of the collection, or returns false.
      Specified by:
      allSatisfyWith in interface RichIterable<T>
      Overrides:
      allSatisfyWith in class AbstractRichIterable<T>
    • noneSatisfy

      public boolean noneSatisfy(Predicate<? super T> predicate)
      Description copied from interface: RichIterable
      Returns true if the predicate evaluates to false for every element of the iterable or if the iterable is empty. Otherwise, returns false.
      Specified by:
      noneSatisfy in interface RichIterable<T>
      Overrides:
      noneSatisfy in class AbstractRichIterable<T>
    • noneSatisfyWith

      public <P> boolean noneSatisfyWith(Predicate2<? super T,? super P> predicate, P parameter)
      Description copied from interface: RichIterable
      Returns true if the predicate evaluates to false for every element of the collection, or return false. Returns true if the collection is empty.
      Specified by:
      noneSatisfyWith in interface RichIterable<T>
      Overrides:
      noneSatisfyWith in class AbstractRichIterable<T>
    • injectInto

      public <IV> IV injectInto(IV injectedValue, Function2<? super IV,? super T,? extends IV> function)
      Description copied from interface: RichIterable
      Returns the final result of evaluating function using each element of the iterable and the previous evaluation result as the parameters. The injected value is used for the first parameter of the first evaluation, and the current item in the iterable is used as the second parameter. This method is commonly called fold or sometimes reduce.
      Specified by:
      injectInto in interface RichIterable<T>
      Overrides:
      injectInto in class AbstractBag<T>
    • injectInto

      public int injectInto(int injectedValue, IntObjectToIntFunction<? super T> function)
      Description copied from interface: RichIterable
      Returns the final int result of evaluating function using each element of the iterable and the previous evaluation result as the parameters. The injected value is used for the first parameter of the first evaluation, and the current item in the iterable is used as the second parameter.
      Specified by:
      injectInto in interface RichIterable<T>
      Overrides:
      injectInto in class AbstractBag<T>
    • injectInto

      public long injectInto(long injectedValue, LongObjectToLongFunction<? super T> function)
      Description copied from interface: RichIterable
      Returns the final long result of evaluating function using each element of the iterable and the previous evaluation result as the parameters. The injected value is used for the first parameter of the first evaluation, and the current item in the iterable is used as the second parameter.
      Specified by:
      injectInto in interface RichIterable<T>
      Overrides:
      injectInto in class AbstractBag<T>
    • injectInto

      public double injectInto(double injectedValue, DoubleObjectToDoubleFunction<? super T> function)
      Description copied from interface: RichIterable
      Returns the final double result of evaluating function using each element of the iterable and the previous evaluation result as the parameters. The injected value is used for the first parameter of the first evaluation, and the current item in the iterable is used as the second parameter.
      Specified by:
      injectInto in interface RichIterable<T>
      Overrides:
      injectInto in class AbstractBag<T>
    • injectInto

      public float injectInto(float injectedValue, FloatObjectToFloatFunction<? super T> function)
      Description copied from interface: RichIterable
      Returns the final float result of evaluating function using each element of the iterable and the previous evaluation result as the parameters. The injected value is used for the first parameter of the first evaluation, and the current item in the iterable is used as the second parameter.
      Specified by:
      injectInto in interface RichIterable<T>
      Overrides:
      injectInto in class AbstractBag<T>
    • equals

      public boolean equals(Object obj)
      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:
    • 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.
      Specified by:
      toMapOfItemToCount in interface Bag<T>
      Specified by:
      toMapOfItemToCount in interface ImmutableBagIterable<T>
    • toSet

      public MutableSet<T> toSet()
      Description copied from interface: RichIterable
      Converts the collection to a MutableSet implementation.
      Specified by:
      toSet in interface RichIterable<T>
      Overrides:
      toSet in class AbstractBag<T>
    • toBag

      public MutableBag<T> toBag()
      Description copied from interface: RichIterable
      Converts the collection to the default MutableBag implementation.
      Specified by:
      toBag in interface RichIterable<T>
      Overrides:
      toBag in class AbstractBag<T>
    • toSortedBag

      public MutableSortedBag<T> toSortedBag()
      Description copied from interface: RichIterable
      Converts the collection to a MutableSortedBag implementation and sorts it using the natural order of the elements.
      Specified by:
      toSortedBag in interface RichIterable<T>
      Overrides:
      toSortedBag in class AbstractBag<T>
    • toSortedBag

      public MutableSortedBag<T> toSortedBag(Comparator<? super T> comparator)
      Description copied from interface: RichIterable
      Converts the collection to the MutableSortedBag implementation and sorts it using the specified comparator.
      Specified by:
      toSortedBag in interface RichIterable<T>
      Overrides:
      toSortedBag in class AbstractBag<T>
    • toSortedBagBy

      public <V extends Comparable<? super V>> MutableSortedBag<T> toSortedBagBy(Function<? super T,? extends V> function)
      Description copied from interface: RichIterable
      Converts the collection to a MutableSortedBag implementation and sorts it based on the natural order of the attribute returned by function.
      Specified by:
      toSortedBagBy in interface RichIterable<T>
      Overrides:
      toSortedBagBy in class AbstractRichIterable<T>
    • asLazy

      public LazyIterable<T> asLazy()
      Description copied from interface: RichIterable
      Returns a lazy (deferred) iterable, most likely implemented by calling LazyIterate.adapt(this).
      Specified by:
      asLazy in interface RichIterable<T>
      Overrides:
      asLazy in class AbstractRichIterable<T>
    • toArray

      public Object[] toArray()
      Description copied from interface: RichIterable
      Converts this iterable to an array.
      Specified by:
      toArray in interface Collection<T>
      Specified by:
      toArray in interface RichIterable<T>
      Overrides:
      toArray in class AbstractRichIterable<T>
      See Also:
    • toArray

      public <T> T[] toArray(T[] a)
      Description copied from interface: RichIterable
      Converts this iterable to an array using the specified target array, assuming the target array is as long or longer than the iterable.
      Specified by:
      toArray in interface Collection<T>
      Specified by:
      toArray in interface RichIterable<T>
      Overrides:
      toArray in class AbstractRichIterable<T>
      See Also:
    • toString

      public String toString()
      Description copied from class: AbstractRichIterable
      Returns a string with the elements of the iterable separated by commas with spaces and enclosed in square brackets.
       Assert.assertEquals("[]", Lists.mutable.empty().toString());
       Assert.assertEquals("[1]", Lists.mutable.with(1).toString());
       Assert.assertEquals("[1, 2, 3]", Lists.mutable.with(1, 2, 3).toString());
       
      Specified by:
      toString in interface RichIterable<T>
      Overrides:
      toString in class AbstractRichIterable<T>
      Returns:
      a string representation of this collection.
      See Also:
    • makeString

      public String makeString()
      Description copied from interface: RichIterable
      Returns a string representation of this collection by delegating to RichIterable.makeString(String) and defaulting the separator parameter to the characters ", " (comma and space).
      Specified by:
      makeString in interface RichIterable<T>
      Returns:
      a string representation of this collection.
    • makeString

      public String makeString(String separator)
      Description copied from interface: RichIterable
      Returns a string representation of this collection by delegating to RichIterable.makeString(String, String, String) and defaulting the start and end parameters to "" (the empty String).
      Specified by:
      makeString in interface RichIterable<T>
      Returns:
      a string representation of this collection.
    • makeString

      public String makeString(String start, String separator, String end)
      Description copied from interface: RichIterable
      Returns a string representation of this collection with the elements separated by the specified separator and enclosed between the start and end strings.
      Specified by:
      makeString in interface RichIterable<T>
      Returns:
      a string representation of this collection.
    • appendString

      public void appendString(Appendable appendable)
      Description copied from interface: RichIterable
      Prints a string representation of this collection onto the given Appendable. Prints the string returned by RichIterable.makeString().
      Specified by:
      appendString in interface RichIterable<T>
    • appendString

      public void appendString(Appendable appendable, String separator)
      Description copied from interface: RichIterable
      Prints a string representation of this collection onto the given Appendable. Prints the string returned by RichIterable.makeString(String).
      Specified by:
      appendString in interface RichIterable<T>
      Overrides:
      appendString in class AbstractRichIterable<T>
    • appendString

      public void appendString(Appendable appendable, String start, String separator, String end)
      Description copied from interface: RichIterable
      Prints a string representation of this collection onto the given Appendable. Prints the string returned by RichIterable.makeString(String, String, String).
      Specified by:
      appendString in interface RichIterable<T>
      Overrides:
      appendString in class AbstractRichIterable<T>
    • zip

      @Deprecated public <S> ImmutableBag<Pair<T,S>> zip(Iterable<S> that)
      Deprecated.
      in 6.0. Use OrderedIterable.zip(Iterable) instead.
      Description copied from interface: RichIterable
      Returns a RichIterable formed from this RichIterable and another RichIterable by combining corresponding elements in pairs. If one of the two RichIterables is longer than the other, its remaining elements are ignored.
      Specified by:
      zip in interface ImmutableBag<T>
      Specified by:
      zip in interface ImmutableCollection<T>
      Specified by:
      zip in interface RichIterable<T>
      Specified by:
      zip in interface UnsortedBag<T>
      Type Parameters:
      S - the type of the second half of the returned pairs
      Parameters:
      that - The RichIterable providing the second half of each result pair
      Returns:
      A new RichIterable containing pairs consisting of corresponding elements of this RichIterable and that. The length of the returned RichIterable is the minimum of the lengths of this RichIterable and that.
    • zip

      public <S, R extends Collection<Pair<T, S>>> R zip(Iterable<S> that, R target)
      Description copied from interface: RichIterable
      Same as RichIterable.zip(Iterable) but uses target for output.
      Specified by:
      zip in interface RichIterable<T>
      Overrides:
      zip in class AbstractRichIterable<T>
    • zipWithIndex

      @Deprecated public ImmutableSet<Pair<T,Integer>> zipWithIndex()
      Deprecated.
      in 6.0. Use OrderedIterable.zipWithIndex() instead.
      Description copied from interface: RichIterable
      Zips this RichIterable with its indices.
      Specified by:
      zipWithIndex in interface Bag<T>
      Specified by:
      zipWithIndex in interface ImmutableBag<T>
      Specified by:
      zipWithIndex in interface ImmutableBagIterable<T>
      Specified by:
      zipWithIndex in interface ImmutableCollection<T>
      Specified by:
      zipWithIndex in interface RichIterable<T>
      Specified by:
      zipWithIndex in interface UnsortedBag<T>
      Returns:
      A new RichIterable containing pairs consisting of all elements of this RichIterable paired with their index. Indices start at 0.
      See Also:
    • zipWithIndex

      public <R extends Collection<Pair<T, Integer>>> R zipWithIndex(R target)
      Description copied from interface: RichIterable
      Same as RichIterable.zipWithIndex() but uses target for output.
      Specified by:
      zipWithIndex in interface RichIterable<T>
      Overrides:
      zipWithIndex in class AbstractRichIterable<T>
    • iterator

      public Iterator<T> iterator()
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<T>