Class ImmutableArrayBag<T>

    • Field Detail

      • MAXIMUM_USEFUL_ARRAY_BAG_SIZE

        static final int MAXIMUM_USEFUL_ARRAY_BAG_SIZE
        See Also:
        Constant Field Values
      • keys

        private final T[] keys
      • counts

        private final int[] counts
    • Constructor Detail

      • ImmutableArrayBag

        ImmutableArrayBag​(T[] keys,
                          int[] counts)
    • Method Detail

      • 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 java.util.Collection<T>
        Specified by:
        size in interface RichIterable<T>
      • occurrencesOf

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

        private ImmutableBag<T> newArrayBagWith​(T element,
                                                int elementIndex,
                                                int distinctItemCount)
      • newWithAll

        public ImmutableBag<T> newWithAll​(java.lang.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>
      • 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>
      • 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 java.lang.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​(java.lang.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 java.util.Collection<T>
        Overrides:
        equals in class java.lang.Object
        See Also:
        Map.equals(Object)
      • hashCode

        public int hashCode()
        Description copied from interface: Bag
        Returns the hash code for this Bag, defined as this.Bag.toMapOfItemToCount().hashCode().
        Specified by:
        hashCode in interface Bag<T>
        Specified by:
        hashCode in interface java.util.Collection<T>
        Overrides:
        hashCode in class java.lang.Object
        See Also:
        Map.hashCode()
      • 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:
        InternalIterable.forEach(Procedure), Iterable.forEach(java.util.function.Consumer)
      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.util.Collection<T>
        Specified by:
        iterator in interface java.lang.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>
      • 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 java.util.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> java.util.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​(java.util.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​(java.util.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>
      • minBy

        public <V extends java.lang.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 java.lang.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​(java.lang.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.
      • writeReplace

        protected java.lang.Object writeReplace()
      • 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.