Package org.cactoos.proc
Class ForEachWithIndex<X>
- java.lang.Object
-
- org.cactoos.proc.ForEachWithIndex<X>
-
- Type Parameters:
X
- The type to iterate over
public final class ForEachWithIndex<X> extends Object implements Proc<Iterable<X>>
Executes aBiProc
for each element of anIterable
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 Summary
Constructors Constructor Description ForEachWithIndex(BiProc<X,Integer> proc)
Ctor.
-