Class CollectIterable<T,​V>

All Implemented Interfaces:
Iterable<V>, InternalIterable<V>, LazyIterable<V>, RichIterable<V>

public class CollectIterable<T,​V>
extends AbstractLazyIterable<V>
A CollectIterable is an iterable that transforms a source iterable using a function as it iterates.
  • Constructor Details

  • Method Details

    • each

      public void each​(Procedure<? super V> 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).
      See Also:
      InternalIterable.forEach(Procedure), Iterable.forEach(java.util.function.Consumer)
    • forEachWithIndex

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

      Example using a Java 8 lambda:

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

      Example using an anonymous inner class:

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

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

      Example using a Java 8 lambda:

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

      Example using an anonymous inner class:

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

      public Iterator<V> iterator()
    • size

      public int size()
      Description copied from interface: RichIterable
      Returns the number of items in this iterable.
      Specified by:
      size in interface RichIterable<T>
      Overrides:
      size in class AbstractLazyIterable<V>
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: RichIterable
      Returns true if this iterable has zero items.
      Specified by:
      isEmpty in interface RichIterable<T>
      Overrides:
      isEmpty in class AbstractLazyIterable<V>
    • notEmpty

      public boolean notEmpty()
      Description copied from interface: RichIterable
      The English equivalent of !this.isEmpty()
    • toArray

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

      public boolean anySatisfy​(Predicate<? super V> 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<V>
    • allSatisfy

      public boolean allSatisfy​(Predicate<? super V> 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<V>
    • noneSatisfy

      public boolean noneSatisfy​(Predicate<? super V> 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<V>
    • detect

      public V detect​(Predicate<? super V> 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<V>
    • detectOptional

      public Optional<V> detectOptional​(Predicate<? super V> 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<V>
    • injectInto

      public <IV> IV injectInto​(IV injectedValue, Function2<? super IV,​? super V,​? extends IV> f)
      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 AbstractRichIterable<V>
    • injectInto

      public int injectInto​(int injectedValue, IntObjectToIntFunction<? super V> f)
      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 AbstractRichIterable<V>
    • injectInto

      public long injectInto​(long injectedValue, LongObjectToLongFunction<? super V> f)
      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 AbstractRichIterable<V>
    • injectInto

      public double injectInto​(double injectedValue, DoubleObjectToDoubleFunction<? super V> f)
      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<V>
    • injectInto

      public float injectInto​(float injectedValue, FloatObjectToFloatFunction<? super V> f)
      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 AbstractRichIterable<V>
    • getFirst

      public V 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 LazyIterable<T>
      Specified by:
      getFirst in interface RichIterable<T>
      Overrides:
      getFirst in class AbstractLazyIterable<V>
    • getLast

      public V 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>
      Overrides:
      getLast in class AbstractLazyIterable<V>