Class FastList<T>

  • All Implemented Interfaces:
    java.io.Externalizable, java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<T>, java.util.Collection<T>, java.util.List<T>, java.util.RandomAccess, MutableCollection<T>, InternalIterable<T>, ListIterable<T>, MutableList<T>, OrderedIterable<T>, ReversibleIterable<T>, RichIterable<T>, BatchIterable<T>

    public class FastList<T>
    extends AbstractMutableList<T>
    implements java.io.Externalizable, java.util.RandomAccess, BatchIterable<T>
    FastList is an attempt to provide the same functionality as ArrayList without the support for concurrent modification exceptions. It also attempts to correct the problem with subclassing ArrayList in that the data elements are protected, not private. It is this issue that caused this class to be created in the first place. The intent was to provide optimized internal iterators which use direct access against the array of items, which is currently not possible by subclassing ArrayList.

    An empty FastList created by calling the default constructor starts with a shared reference to a static empty array (DEFAULT_SIZED_EMPTY_ARRAY). This makes empty FastLists very memory efficient. The first call to add will lazily create an array of size 10.

    An empty FastList created by calling the pre-size constructor with a value of 0 (new FastList(0)) starts with a shared reference to a static empty array (ZERO_SIZED_ARRAY). This makes FastLists presized to 0 very memory efficient as well. The first call to add will lazily create an array of size 1.

    See Also:
    Serialized Form
    • Field Detail

      • DEFAULT_SIZED_EMPTY_ARRAY

        private static final java.lang.Object[] DEFAULT_SIZED_EMPTY_ARRAY
      • ZERO_SIZED_ARRAY

        private static final java.lang.Object[] ZERO_SIZED_ARRAY
      • size

        protected int size
      • items

        protected transient T[] items
    • Constructor Detail

      • FastList

        public FastList()
      • FastList

        public FastList​(int initialCapacity)
      • FastList

        protected FastList​(T[] array)
      • FastList

        protected FastList​(int size,
                           T[] array)
      • FastList

        public FastList​(java.util.Collection<? extends T> source)
    • Method Detail

      • newList

        public static <E> FastList<E> newList()
      • wrapCopy

        public static <E> FastList<E> wrapCopy​(E... array)
      • newList

        public static <E> FastList<E> newList​(int initialCapacity)
      • newList

        public static <E> FastList<E> newList​(java.lang.Iterable<? extends E> source)
      • newListWith

        public static <E> FastList<E> newListWith​(E... elements)
        Creates a new list using the passed elements argument as the backing store.

        !!! WARNING: This method uses the passed in array, so can be very unsafe if the original array is held onto anywhere else. !!!

      • newWithNValues

        public static <E> FastList<E> newWithNValues​(int size,
                                                     Function0<? extends E> factory)
        Creates a new FastList pre-sized to the specified size filled with default values generated by the specified function.
        Since:
        3.0
      • removeRange

        public void removeRange​(int fromIndex,
                                int toIndex)
        Removes from this list all the elements whose index is between fromIndex, inclusive, and toIndex, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by (toIndex - fromIndex) elements. (If toIndex==fromIndex, this operation has no effect.)
        Overrides:
        removeRange in class AbstractMutableList<T>
        Parameters:
        fromIndex - inclusive
        toIndex - exclusive
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<T>
        Specified by:
        clear in interface java.util.List<T>
        Overrides:
        clear in class AbstractMutableList<T>
      • forEach

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

        This code would output ted and mary's names.

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

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

        This code would output ted and mary's names.

        Specified by:
        forEachWithIndex in interface OrderedIterable<T>
        Overrides:
        forEachWithIndex in class AbstractMutableList<T>
      • toArray

        public <E> E[] toArray​(E[] array,
                               int sourceFromIndex,
                               int sourceToIndex,
                               int destinationIndex)
      • toArray

        public <E> E[] toArray​(int sourceFromIndex,
                               int sourceToIndex)
      • sort

        public void sort​(java.util.Comparator<? super T> comparator)
        Overrides default method from List.
        Specified by:
        sort in interface java.util.List<T>
        Since:
        10.0 - Overridden for efficiency
      • sortThis

        public FastList<T> sortThis​(java.util.Comparator<? super T> comparator)
        Description copied from interface: MutableList
        Sorts the internal data structure of this list and returns the list itself as a convenience.
        Specified by:
        sortThis in interface MutableList<T>
      • sortThis

        public FastList<T> sortThis()
        Description copied from interface: MutableList
        Sorts the internal data structure of this list and returns the list itself as a convenience.
        Specified by:
        sortThis in interface MutableList<T>
      • reverseThis

        public FastList<T> reverseThis()
        Description copied from interface: MutableList
        Mutates this list by reversing its order and returns the current list as a result.
        Specified by:
        reverseThis in interface MutableList<T>
      • addAll

        public boolean addAll​(java.util.Collection<? extends T> source)
        Specified by:
        addAll in interface java.util.Collection<T>
        Specified by:
        addAll in interface java.util.List<T>
        Overrides:
        addAll in class AbstractMutableCollection<T>
      • addAllFastList

        private void addAllFastList​(FastList<T> source)
      • addAllArrayList

        private void addAllArrayList​(java.util.ArrayList<T> source)
      • addAllRandomAccessList

        private void addAllRandomAccessList​(java.util.List<T> source)
      • addAllCollection

        private void addAllCollection​(java.util.Collection<? extends T> source)
      • toArray

        public <E> E[] toArray​(E[] array)
        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 java.util.Collection<T>
        Specified by:
        toArray in interface java.util.List<T>
        Specified by:
        toArray in interface MutableCollection<T>
        Specified by:
        toArray in interface MutableList<T>
        Specified by:
        toArray in interface RichIterable<T>
        See Also:
        Collection.toArray(Object[])
      • toArray

        public java.lang.Object[] toArray()
        Description copied from interface: RichIterable
        Converts this iterable to an array.
        Specified by:
        toArray in interface java.util.Collection<T>
        Specified by:
        toArray in interface java.util.List<T>
        Specified by:
        toArray in interface MutableCollection<T>
        Specified by:
        toArray in interface MutableList<T>
        Specified by:
        toArray in interface RichIterable<T>
        See Also:
        Collection.toArray()
      • toTypedArray

        public T[] toTypedArray​(java.lang.Class<T> clazz)
      • throwOutOfBounds

        private void throwOutOfBounds​(int index)
      • set

        public T set​(int index,
                     T element)
        Specified by:
        set in interface java.util.List<T>
      • indexOf

        public int indexOf​(java.lang.Object object)
        Description copied from interface: OrderedIterable
        Returns the index of the first occurrence of the specified item in this iterable, or -1 if this iterable does not contain the item.
        Specified by:
        indexOf in interface java.util.List<T>
        Specified by:
        indexOf in interface MutableList<T>
        Specified by:
        indexOf in interface OrderedIterable<T>
        Overrides:
        indexOf in class AbstractMutableList<T>
        See Also:
        List.indexOf(Object)
      • lastIndexOf

        public int lastIndexOf​(java.lang.Object object)
        Description copied from interface: ListIterable
        Returns the index of the last occurrence of the specified item in this list, or -1 if this list does not contain the item.
        Specified by:
        lastIndexOf in interface java.util.List<T>
        Specified by:
        lastIndexOf in interface ListIterable<T>
        Overrides:
        lastIndexOf in class AbstractMutableList<T>
      • spliterator

        public java.util.Spliterator<T> spliterator()
        Specified by:
        spliterator in interface java.util.Collection<T>
        Specified by:
        spliterator in interface java.lang.Iterable<T>
        Specified by:
        spliterator in interface java.util.List<T>
        Since:
        8.1
      • trimToSize

        public void trimToSize()
      • trimToSizeIfGreaterThanPercent

        public boolean trimToSizeIfGreaterThanPercent​(double loadFactor)
        Express load factor as 0.25 to trim a collection with more than 25% excess capacity
      • ensureCapacityForAdd

        private void ensureCapacityForAdd()
      • ensureCapacityForAddAll

        private int ensureCapacityForAddAll​(java.util.Collection<T> source)
      • ensureCapacity

        public void ensureCapacity​(int minCapacity)
      • transferItemsToNewArrayWithCapacity

        private void transferItemsToNewArrayWithCapacity​(int newCapacity)
      • copyItemsWithNewCapacity

        private java.lang.Object[] copyItemsWithNewCapacity​(int newCapacity)
      • with

        public FastList<T> with​(T element1,
                                T element2)
      • with

        public FastList<T> with​(T element1,
                                T element2,
                                T element3)
      • with

        public FastList<T> with​(T... elements)
      • withArrayCopy

        public FastList<T> withArrayCopy​(T[] elements,
                                         int begin,
                                         int length)
      • groupBy

        public <V> FastListMultimap<V,​T> groupBy​(Function<? super T,​? extends V> function)
        Description copied from interface: MutableCollection
        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);
         
        Co-variant example for MutableCollection:
         MutableMultimap<String, Person> peopleByLastName =
             people.groupBy(Person::getLastName);
         
        Specified by:
        groupBy in interface ListIterable<T>
        Specified by:
        groupBy in interface MutableCollection<T>
        Specified by:
        groupBy in interface MutableList<T>
        Specified by:
        groupBy in interface OrderedIterable<T>
        Specified by:
        groupBy in interface ReversibleIterable<T>
        Specified by:
        groupBy in interface RichIterable<T>
        Overrides:
        groupBy in class AbstractMutableList<T>
      • forEachIf

        public void forEachIf​(Predicate<? super T> predicate,
                              Procedure<? super T> procedure)
      • forEachWithIndex

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

        Example using a Java 8 lambda:

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

        Example using an anonymous inner class:

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

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

        Example using a Java 8 lambda:

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

        Example using an anonymous inner class:

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

        public <R extends java.util.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());
         

        Specified by:
        select in interface RichIterable<T>
        Overrides:
        select in class AbstractMutableList<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:
        RichIterable.select(Predicate)
      • selectWith

        public <P,​R extends java.util.Collection<T>> R selectWith​(Predicate2<? super T,​? super P> predicate,
                                                                        P parameter,
                                                                        R target)
        Description copied from interface: RichIterable
        Similar to RichIterable.select(Predicate, Collection), except with an evaluation parameter for the second generic argument in Predicate2.

        E.g. return a Collection of Person elements where the person has an age greater than or equal to 18 years

        Example using a Java 8 lambda expression:

         MutableList<Person> selected =
             people.selectWith((Person person, Integer age) -> person.getAge()>= age, Integer.valueOf(18), Lists.mutable.empty());
         
        Specified by:
        selectWith in interface RichIterable<T>
        Overrides:
        selectWith in class AbstractMutableList<T>
        Parameters:
        predicate - a Predicate2 to use as the select criteria
        parameter - a parameter to pass in for evaluation of the second argument P in predicate
        target - the Collection to append to for all elements in this RichIterable that meet select criteria predicate
        Returns:
        targetCollection, which contains appended elements as a result of the select criteria
        See Also:
        RichIterable.select(Predicate), RichIterable.select(Predicate, Collection)
      • reject

        public <R extends java.util.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());
         
        Specified by:
        reject in interface RichIterable<T>
        Overrides:
        reject in class AbstractMutableList<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
      • rejectWith

        public <P,​R extends java.util.Collection<T>> R rejectWith​(Predicate2<? super T,​? super P> predicate,
                                                                        P parameter,
                                                                        R target)
        Description copied from interface: RichIterable
        Similar to RichIterable.reject(Predicate, Collection), except with an evaluation parameter for the second generic argument in Predicate2.

        E.g. return a Collection of Person elements where the person has an age greater than or equal to 18 years

        Example using a Java 8 lambda expression:

         MutableList<Person> rejected =
             people.rejectWith((Person person, Integer age) -> person.getAge() < age, Integer.valueOf(18), Lists.mutable.empty());
         
        Specified by:
        rejectWith in interface RichIterable<T>
        Overrides:
        rejectWith in class AbstractMutableList<T>
        Parameters:
        predicate - a Predicate2 to use as the reject criteria
        parameter - a parameter to pass in for evaluation of the second argument P in predicate
        target - the Collection to append to for all elements in this RichIterable that cause Predicate#accept(Object) method to evaluate to false
        Returns:
        targetCollection, which contains appended elements as a result of the reject criteria
        See Also:
        RichIterable.reject(Predicate), RichIterable.reject(Predicate, Collection)
      • wipeAndResetTheEnd

        private void wipeAndResetTheEnd​(int newCurrentFilledIndex)
      • removeIfWith

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

        public <R extends MutableBooleanCollection> R collectBoolean​(BooleanFunction<? super T> booleanFunction,
                                                                     R target)
        Description copied from interface: RichIterable
        Same as RichIterable.collectBoolean(BooleanFunction), except that the results are gathered into the specified target collection.

        Example using a Java 8 lambda expression:

         BooleanArrayList licenses =
             people.collectBoolean(person -> person.hasDrivingLicense(), new BooleanArrayList());
         
        Specified by:
        collectBoolean in interface RichIterable<T>
        Parameters:
        booleanFunction - a BooleanFunction to use as the collect transformation function
        target - the MutableBooleanCollection to append to for all elements in this RichIterable
        Returns:
        target, which contains appended elements as a result of the collect transformation
      • collectByte

        public <R extends MutableByteCollection> R collectByte​(ByteFunction<? super T> byteFunction,
                                                               R target)
        Description copied from interface: RichIterable
        Same as RichIterable.collectByte(ByteFunction), except that the results are gathered into the specified target collection.

        Example using a Java 8 lambda expression:

         ByteArrayList bytes =
             people.collectByte(person -> person.getCode(), new ByteArrayList());
         
        Specified by:
        collectByte in interface RichIterable<T>
        Parameters:
        byteFunction - a ByteFunction to use as the collect transformation function
        target - the MutableByteCollection to append to for all elements in this RichIterable
        Returns:
        target, which contains appended elements as a result of the collect transformation
      • collectChar

        public <R extends MutableCharCollection> R collectChar​(CharFunction<? super T> charFunction,
                                                               R target)
        Description copied from interface: RichIterable
        Same as RichIterable.collectChar(CharFunction), except that the results are gathered into the specified target collection.

        Example using a Java 8 lambda expression:

         CharArrayList chars =
             people.collectChar(person -> person.getMiddleInitial(), new CharArrayList());
         
        Specified by:
        collectChar in interface RichIterable<T>
        Parameters:
        charFunction - a CharFunction to use as the collect transformation function
        target - the MutableCharCollection to append to for all elements in this RichIterable
        Returns:
        target, which contains appended elements as a result of the collect transformation
      • collectDouble

        public <R extends MutableDoubleCollection> R collectDouble​(DoubleFunction<? super T> doubleFunction,
                                                                   R target)
        Description copied from interface: RichIterable
        Same as RichIterable.collectDouble(DoubleFunction), except that the results are gathered into the specified target collection.

        Example using a Java 8 lambda expression:

         DoubleArrayList doubles =
             people.collectDouble(person -> person.getMilesFromNorthPole(), new DoubleArrayList());
         
        Specified by:
        collectDouble in interface RichIterable<T>
        Parameters:
        doubleFunction - a DoubleFunction to use as the collect transformation function
        target - the MutableDoubleCollection to append to for all elements in this RichIterable
        Returns:
        target, which contains appended elements as a result of the collect transformation
      • collectFloat

        public <R extends MutableFloatCollection> R collectFloat​(FloatFunction<? super T> floatFunction,
                                                                 R target)
        Description copied from interface: RichIterable
        Same as RichIterable.collectFloat(FloatFunction), except that the results are gathered into the specified target collection.

        Example using a Java 8 lambda expression:

         FloatArrayList floats =
             people.collectFloat(person -> person.getHeightInInches(), new FloatArrayList());
         
        Specified by:
        collectFloat in interface RichIterable<T>
        Parameters:
        floatFunction - a FloatFunction to use as the collect transformation function
        target - the MutableFloatCollection to append to for all elements in this RichIterable
        Returns:
        target, which contains appended elements as a result of the collect transformation
      • collectInt

        public <R extends MutableIntCollection> R collectInt​(IntFunction<? super T> intFunction,
                                                             R target)
        Description copied from interface: RichIterable
        Same as RichIterable.collectInt(IntFunction), except that the results are gathered into the specified target collection.

        Example using a Java 8 lambda expression:

         IntArrayList ints =
             people.collectInt(person -> person.getAge(), new IntArrayList());
         
        Specified by:
        collectInt in interface RichIterable<T>
        Parameters:
        intFunction - a IntFunction to use as the collect transformation function
        target - the MutableIntCollection to append to for all elements in this RichIterable
        Returns:
        target, which contains appended elements as a result of the collect transformation
      • collectLong

        public <R extends MutableLongCollection> R collectLong​(LongFunction<? super T> longFunction,
                                                               R target)
        Description copied from interface: RichIterable
        Same as RichIterable.collectLong(LongFunction), except that the results are gathered into the specified target collection.

        Example using a Java 8 lambda expression:

         LongArrayList longs =
             people.collectLong(person -> person.getGuid(), new LongArrayList());
         
        Specified by:
        collectLong in interface RichIterable<T>
        Parameters:
        longFunction - a LongFunction to use as the collect transformation function
        target - the MutableLongCollection to append to for all elements in this RichIterable
        Returns:
        target, which contains appended elements as a result of the collect transformation
      • collectShort

        public <R extends MutableShortCollection> R collectShort​(ShortFunction<? super T> shortFunction,
                                                                 R target)
        Description copied from interface: RichIterable
        Same as RichIterable.collectShort(ShortFunction), except that the results are gathered into the specified target collection.

        Example using a Java 8 lambda expression:

         ShortArrayList shorts =
             people.collectShort(person -> person.getNumberOfJunkMailItemsReceivedPerMonth, new ShortArrayList());
         
        Specified by:
        collectShort in interface RichIterable<T>
        Parameters:
        shortFunction - a ShortFunction to use as the collect transformation function
        target - the MutableShortCollection to append to for all elements in this RichIterable
        Returns:
        target, which contains appended elements as a result of the collect transformation
      • collect

        public <V,​R extends java.util.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());
         
        Specified by:
        collect in interface RichIterable<T>
        Overrides:
        collect in class AbstractMutableList<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:
        RichIterable.collect(Function)
      • collectWithIndex

        public <V,​R extends java.util.Collection<V>> R collectWithIndex​(ObjectIntToObjectFunction<? super T,​? extends V> function,
                                                                              R target)
        Description copied from interface: OrderedIterable
        Adds elements to the target Collection using results obtained by applying the specified function to each element and its corresponding index.
        Specified by:
        collectWithIndex in interface OrderedIterable<T>
        Since:
        9.1.
      • selectWithIndex

        public <R extends java.util.Collection<T>> R selectWithIndex​(ObjectIntPredicate<? super T> predicate,
                                                                     R target)
        Description copied from interface: OrderedIterable
        Adds all elements to the target Collection that return true when evaluating the specified predicate which is supplied each element and its relative index.
        Specified by:
        selectWithIndex in interface OrderedIterable<T>
        Since:
        11.0
      • rejectWithIndex

        public <R extends java.util.Collection<T>> R rejectWithIndex​(ObjectIntPredicate<? super T> predicate,
                                                                     R target)
        Description copied from interface: OrderedIterable
        Adds all elements to the target Collection that return false when evaluating the specified predicate which is supplied each element and its relative index.
        Specified by:
        rejectWithIndex in interface OrderedIterable<T>
        Since:
        11.0
      • flatCollect

        public <V> FastList<V> flatCollect​(Function<? super T,​? extends java.lang.Iterable<V>> function)
        Description copied from interface: MutableCollection
        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);
         
        Co-variant example for MutableCollection:
         Function<Person, List<Address>> addressFunction = Person::getAddresses;
         MutableCollection<Person> people = ...;
         MutableCollection<List<Address>> addresses = people.collect(addressFunction);
         MutableCollection<Address> addresses = people.flatCollect(addressFunction);
         
        Specified by:
        flatCollect in interface ListIterable<T>
        Specified by:
        flatCollect in interface MutableCollection<T>
        Specified by:
        flatCollect in interface MutableList<T>
        Specified by:
        flatCollect in interface OrderedIterable<T>
        Specified by:
        flatCollect in interface ReversibleIterable<T>
        Specified by:
        flatCollect in interface RichIterable<T>
        Parameters:
        function - The Function to apply
        Returns:
        a new flattened collection produced by applying the given function
      • flatCollect

        public <V,​R extends java.util.Collection<V>> R flatCollect​(Function<? super T,​? extends java.lang.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 AbstractMutableList<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:
        RichIterable.flatCollect(Function)
      • collectWith

        public <P,​V,​R extends java.util.Collection<V>> R collectWith​(Function2<? super T,​? super P,​? extends V> function,
                                                                                 P parameter,
                                                                                 R target)
        Description copied from interface: RichIterable
        Same as collectWith but with a targetCollection parameter to gather the results.

        Example using a Java 8 lambda expression:

         MutableSet<Integer> integers =
             Lists.mutable.with(1, 2, 3).collectWith((each, parameter) -> each + parameter, Integer.valueOf(1), Sets.mutable.empty());
         
        Specified by:
        collectWith in interface RichIterable<T>
        Overrides:
        collectWith in class AbstractMutableList<T>
        Parameters:
        function - a Function2 to use as the collect transformation function
        parameter - a parameter to pass in for evaluation of the second argument P in function
        target - the Collection to append to for all elements in this RichIterable that meet select criteria function
        Returns:
        targetCollection, which contains appended elements as a result of the collect transformation
      • collectIf

        public <V,​R extends java.util.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 AbstractMutableList<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:
        RichIterable.collectIf(Predicate, Function)
      • 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"));
         
        Specified by:
        detect in interface RichIterable<T>
        Overrides:
        detect in class AbstractMutableList<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");
         
        Specified by:
        detectWith in interface RichIterable<T>
        Overrides:
        detectWith in class AbstractMutableList<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 AbstractMutableList<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 AbstractMutableList<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 AbstractMutableList<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 AbstractMutableList<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 AbstractMutableList<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 AbstractMutableList<T>
      • get

        public T get​(int index)
        Description copied from interface: ListIterable
        Returns the item at the specified position in this list iterable.
        Specified by:
        get in interface java.util.List<T>
        Specified by:
        get in interface ListIterable<T>
      • newIndexOutOfBoundsException

        private java.lang.IndexOutOfBoundsException newIndexOutOfBoundsException​(int index)
      • add

        public boolean add​(T newItem)
        Specified by:
        add in interface java.util.Collection<T>
        Specified by:
        add in interface java.util.List<T>
        Overrides:
        add in class AbstractMutableCollection<T>
      • add

        public void add​(int index,
                        T element)
        Specified by:
        add in interface java.util.List<T>
      • addAtIndex

        private void addAtIndex​(int index,
                                T element)
      • sizePlusFiftyPercent

        private int sizePlusFiftyPercent​(int oldSize)
      • remove

        public T remove​(int index)
        Specified by:
        remove in interface java.util.List<T>
      • remove

        public boolean remove​(java.lang.Object object)
        Specified by:
        remove in interface java.util.Collection<T>
        Specified by:
        remove in interface java.util.List<T>
        Overrides:
        remove in class AbstractMutableCollection<T>
      • addAll

        public boolean addAll​(int index,
                              java.util.Collection<? extends T> source)
        Specified by:
        addAll in interface java.util.List<T>
      • addAllFastListAtIndex

        private void addAllFastListAtIndex​(FastList<T> source,
                                           int index)
      • addAllArrayListAtIndex

        private void addAllArrayListAtIndex​(java.util.ArrayList<T> source,
                                            int index)
      • addAllRandomAccessListAtIndex

        private void addAllRandomAccessListAtIndex​(java.util.List<T> source,
                                                   int index)
      • addAllCollectionAtIndex

        private void addAllCollectionAtIndex​(java.util.Collection<? extends T> source,
                                             int index)
      • shiftElementsAtIndex

        private void shiftElementsAtIndex​(int index,
                                          int sourceSize)
      • size

        public int size()
        Description copied from interface: RichIterable
        Returns the number of items in this iterable.
        Specified by:
        size in interface BatchIterable<T>
        Specified by:
        size in interface java.util.Collection<T>
        Specified by:
        size in interface java.util.List<T>
        Specified by:
        size 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"));
         
        Specified by:
        count in interface RichIterable<T>
        Overrides:
        count in class AbstractMutableList<T>
      • countWith

        public <P> int countWith​(Predicate2<? super T,​? super P> predicate,
                                 P parameter)
        Description copied from interface: RichIterable
        Returns the total number of elements that evaluate to true for the specified predicate.
        e.g.
         return lastNames.countWith(Predicates2.equal(), "Smith");
         
        Specified by:
        countWith in interface RichIterable<T>
        Overrides:
        countWith in class AbstractMutableList<T>
      • corresponds

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

        public java.util.IntSummaryStatistics summarizeInt​(IntFunction<? super T> function)
        Description copied from interface: RichIterable
        Returns the result of summarizing the value returned from applying the IntFunction to each element of the iterable.
         IntSummaryStatistics stats =
             Lists.mutable.with(1, 2, 3).summarizeInt(Integer::intValue);
         
        Specified by:
        summarizeInt in interface RichIterable<T>
        Since:
        8.0
      • summarizeFloat

        public java.util.DoubleSummaryStatistics summarizeFloat​(FloatFunction<? super T> function)
        Description copied from interface: RichIterable
        Returns the result of summarizing the value returned from applying the FloatFunction to each element of the iterable.
         DoubleSummaryStatistics stats =
             Lists.mutable.with(1, 2, 3).summarizeFloat(Integer::floatValue);
         
        Specified by:
        summarizeFloat in interface RichIterable<T>
        Since:
        8.0
      • summarizeLong

        public java.util.LongSummaryStatistics summarizeLong​(LongFunction<? super T> function)
        Description copied from interface: RichIterable
        Returns the result of summarizing the value returned from applying the LongFunction to each element of the iterable.
         LongSummaryStatistics stats =
             Lists.mutable.with(1, 2, 3).summarizeLong(Integer::longValue);
         
        Specified by:
        summarizeLong in interface RichIterable<T>
        Since:
        8.0
      • summarizeDouble

        public java.util.DoubleSummaryStatistics summarizeDouble​(DoubleFunction<? super T> function)
        Description copied from interface: RichIterable
        Returns the result of summarizing the value returned from applying the DoubleFunction to each element of the iterable.
         DoubleSummaryStatistics stats =
             Lists.mutable.with(1, 2, 3).summarizeDouble(Integer::doubleValue);
         
        Specified by:
        summarizeDouble in interface RichIterable<T>
        Since:
        8.0
      • reduce

        public java.util.Optional<T> reduce​(java.util.function.BinaryOperator<T> accumulator)
        Description copied from interface: RichIterable
        This method produces the equivalent result as Stream.reduce(BinaryOperator).
        Specified by:
        reduce in interface RichIterable<T>
        Overrides:
        reduce in class AbstractMutableCollection<T>
        Since:
        8.0
      • reduceInPlace

        public <R,​A> R reduceInPlace​(java.util.stream.Collector<? super T,​A,​R> collector)
        Description copied from interface: RichIterable
        This method produces the equivalent result as Stream.collect(Collector).
         MutableObjectLongMap<Integer> map2 =
             Lists.mutable.with(1, 2, 3, 4, 5).reduceInPlace(Collectors2.sumByInt(i -> Integer.valueOf(i % 2), Integer::intValue));
         
        Specified by:
        reduceInPlace in interface RichIterable<T>
        Since:
        8.0
      • reduceInPlace

        public <R> R reduceInPlace​(java.util.function.Supplier<R> supplier,
                                   java.util.function.BiConsumer<R,​? super T> accumulator)
        Description copied from interface: RichIterable
        This method produces the equivalent result as Stream.collect(Supplier, BiConsumer, BiConsumer). The combiner used in collect is unnecessary in the serial case, so is not included in the API.
        Specified by:
        reduceInPlace in interface RichIterable<T>
        Since:
        8.0
      • sumOfFloat

        public double sumOfFloat​(FloatFunction<? super T> function)
        Description copied from interface: RichIterable
        Returns the final double result of evaluating function for each element of the iterable and adding the results together. It uses Kahan summation algorithm to reduce numerical error.
        Specified by:
        sumOfFloat in interface RichIterable<T>
        Overrides:
        sumOfFloat in class AbstractMutableList<T>
      • sumOfDouble

        public double sumOfDouble​(DoubleFunction<? super T> function)
        Description copied from interface: RichIterable
        Returns the final double result of evaluating function for each element of the iterable and adding the results together. It uses Kahan summation algorithm to reduce numerical error.
        Specified by:
        sumOfDouble in interface RichIterable<T>
        Overrides:
        sumOfDouble in class AbstractMutableList<T>
      • injectIntoWith

        public <IV,​P> IV injectIntoWith​(IV injectValue,
                                              Function3<? super IV,​? super T,​? super P,​? extends IV> function,
                                              P parameter)
        Description copied from interface: MutableCollection
        Returns the final result of evaluating function using each element of the iterable, the previous evaluation result and 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. The parameter value is always used as the third parameter to the function call.
        Specified by:
        injectIntoWith in interface MutableCollection<T>
        Overrides:
        injectIntoWith in class AbstractMutableList<T>
        See Also:
        RichIterable.injectInto(Object, Function2)
      • toSortedList

        public FastList<T> toSortedList​(java.util.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>
      • detectNotIndex

        private int detectNotIndex​(Predicate<? super T> predicate)
      • equals

        public boolean equals​(java.lang.Object that)
        Description copied from interface: ListIterable
        Follows the same general contract as List.equals(Object).
        Specified by:
        equals in interface java.util.Collection<T>
        Specified by:
        equals in interface java.util.List<T>
        Specified by:
        equals in interface ListIterable<T>
        Overrides:
        equals in class AbstractMutableList<T>
      • fastListEquals

        public boolean fastListEquals​(FastList<?> that)
      • hashCode

        public int hashCode()
        Description copied from interface: ListIterable
        Follows the same general contract as List.hashCode().
        Specified by:
        hashCode in interface java.util.Collection<T>
        Specified by:
        hashCode in interface java.util.List<T>
        Specified by:
        hashCode in interface ListIterable<T>
        Overrides:
        hashCode in class AbstractMutableList<T>
      • replaceAll

        public void replaceAll​(java.util.function.UnaryOperator<T> operator)
        Specified by:
        replaceAll in interface java.util.List<T>
        Since:
        10.0
      • writeExternal

        public void writeExternal​(java.io.ObjectOutput out)
                           throws java.io.IOException
        Specified by:
        writeExternal in interface java.io.Externalizable
        Throws:
        java.io.IOException
      • readExternal

        public void readExternal​(java.io.ObjectInput in)
                          throws java.io.IOException,
                                 java.lang.ClassNotFoundException
        Specified by:
        readExternal in interface java.io.Externalizable
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException