Created
September 23, 2019 11:56
-
-
Save Mukundhan-I2I/140877b9366597a51aa7f4e85c9cb80b to your computer and use it in GitHub Desktop.
callback in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WithIndex<T> { | |
private int index; | |
private T value; | |
WithIndex(int index, T value) { | |
this.index = index; | |
this.value = value; | |
} | |
public int index() { | |
return index; | |
} | |
public T value() { | |
return value; | |
} | |
@Override | |
public String toString() { | |
return value + "(" + index + ")"; | |
} | |
public static <T> Function<T, WithIndex<T>> indexed() { | |
return new Function<T, WithIndex<T>>() { | |
int index = 0; | |
@Override | |
public WithIndex<T> apply(T t) { | |
return new WithIndex<>(index++, t); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/questions/18552005/is-there-a-concise-way-to-iterate-over-a-stream-with-indices-in-java-8