Class ForEachWithIndex<X>

  • Type Parameters:
    X - The type to iterate over
    All Implemented Interfaces:
    Proc<Iterable<X>>

    public final class ForEachWithIndex<X>
    extends Object
    implements Proc<Iterable<X>>
    Executes a BiProc for each element of an Iterable

    This class can be effectively used to iterate through a collection similar to how Stream.forEach(java.util.function.Consumer) works except this class also passes the index of the current element to the proc:

    new ForEachWithIndex( new BiProcOf<>((input, index) -> System.out.printf("%d: \'%s\' ", index + 1, input)), ).execute( new IterableOf<>("Mary", "John", "William", "Napkin") ); // will print 1: 'Mary' 2: 'John' 3: 'William' 4: 'Napkin' to standard output

    There is no thread-safety guarantee.

    Since:
    1.0
    • Constructor Detail

      • ForEachWithIndex

        public ForEachWithIndex​(BiProc<X,​Integer> proc)
        Ctor.
        Parameters:
        proc - The proc to execute
    • Method Detail

      • exec

        public void exec​(Iterable<X> input)
                  throws Exception
        Description copied from interface: Proc
        Execute it.
        Specified by:
        exec in interface Proc<X>
        Parameters:
        input - The argument
        Throws:
        Exception - If fails