I want to use java8 forEach with index

When using the java8 forEach method, you may want an index (serial number). At that time, if you try to use the counter variable, you cannot update it in the lambda, so you have to use an array.

I can't do this

int i = 1;
java.util.stream.Stream.of("a", "b", "c", "d").forEach(s -> {
  System.out.println(i + ": " + s);

You can do this

int i[] = { 1 };
java.util.stream.Stream.of("a", "b", "c", "d").forEach(s -> {
  System.out.println(i[0] + ": " + s);

Either way, the outlook is not good, so I want to make it look like this.

I want to do this

java.util.stream.Stream.of("a", "b", "c", "d").forEach(withIndex((s, i) -> System.out.println(i + ": " + s)));

So, let's create a utility method.

Class name and method name are appropriate

import java.util.function.Consumer;
import java.util.function.ObjIntConsumer;

public class With {
  public static <T> Consumer<T> B(int start, ObjIntConsumer<T> consumer) {
    int counter[] = { start };
    return obj -> consumer.accept(obj, counter[0]++);

The initial value can also be set.

did it

java.util.stream.Stream.of("a", "b", "c", "d").forEach(With.B(1, (s, i) -> System.out.println(i + ": " + s)));

Execution result

1: a
2: b
3: c
4: d

[Caution] When executing in parallel in stream, please devise such as using forEachOrdered.

that's all

Recommended Posts

I want to use java8 forEach with index
I want to use DBViewer with Eclipse 2018-12! !!
I want to ForEach an array with a Lambda expression in Java
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I want to transition screens with kotlin and java!
I want to get along with Map [Java beginner]
I want to use FormObject well
I tried to interact with Java
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I want to use the Java 8 DateTime API slowly (now)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to implement various functions with kotlin and java!
[Java] I want to test standard input & standard output with JUnit
I want to stop Java updates altogether
I want to use @Autowired in Servlet
I want to return to the previous screen with kotlin and java!
[Java] I want to perform distinct with the key in the object
I want to write a loop that references an index with Java 8's Stream API
I want to test Action Cable with RSpec test
Run R from Java I want to run rJava
I tried to make Basic authentication with Java
I want to send an email in Java.
I want to use arrow notation in Ruby
I want to display images with REST Controller of Java and Spring!
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
How to use Java framework with AWS Lambda! ??
I want to create a dark web SNS with Jakarta EE 8 with Java 11
rsync4j --I want to touch rsync in Java.
How to use Java API with lambda expression
I want to play with Firestore from Rails
I want to write quickly from java to sqlite
[Java Spring MVC] I want to use DI in my own class
I tried to break a block with java (1)
I want to perform aggregation processing with spring-batch
[Rails] I want to load CSS with webpacker
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
I want you to use Scala as Better Java for the time being
[Eclipse] I want to use the completion function, but I want to manage to confirm the completion with spaces.
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
I want to use Combine in UIKit as well.
I want to use Clojure's convenient functions in Kotlin
I want to do something like "cls" in Java
[Java] I want to calculate the difference from the date
I want to use NetBeans on Mac → I can use it!
I tried to implement TCP / IP + BIO with JAVA
[Java 11] I tried to execute Java without compiling with javac
I want to use fish shell in Laradock too! !!
I want to use a little icon in Rails
I want to dark mode with the SWT app
I want to monitor a specific file with WatchService
I want to authenticate users to Rails with Devise + OmniAuth
If you want to use Mockito with Kotlin, use mockito-kotlin
[Android Studio] I want to use Maven library on Android
I tried to implement Stalin sort with Java Collector
I used to make nc (netcat) with JAVA normally
I want to redirect sound from Ubuntu with xrdp
Use Lambda Layers with Java
[Java] How to use Map
How to use java Optional
How to use java class