Class ImmutableArrayBag<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 ImmutableArrayBag<T> extends AbstractImmutableBag<T> implements Serializable
Since:
1.0
See Also:
  • Method Details

    • newBagWith

      public static <T> ImmutableArrayBag<T> newBagWith(T... elements)
    • copyFrom

      public static <T> ImmutableArrayBag<T> copyFrom(Bag<? extends T> 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.
      Specified by:
      forEachWithOccurrences 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>
    • 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>
    • 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>
    • 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>
    • 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>
    • 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>
    • 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>
    • 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>
    • 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>
    • 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>
    • 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>
    • 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.
    • 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>
    • 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
    • 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>
    • 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 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>
    • 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
    • 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:
    • 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:
    • iterator

      public Iterator<T> iterator()
      Specified by:
      iterator in interface Collection<T>
      Specified by:
      iterator in interface Iterable<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>
    • 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>
    • 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>
    • 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.
    • 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:
    • distinctView

      public RichIterable<T> distinctView()
      Description copied from interface: Bag
      Returns an unmodifiable view on the distinct elements with the same complexity as the Bag implementation.
      Specified by:
      distinctView in interface Bag<T>
      Returns:
      an unmodifiable view on the distinct elements of the Bag.