Skip to content

Instantly share code, notes, and snippets.

@songzhiyong
Forked from jthoenes/LambdaIntro.java
Created December 3, 2013 05:32
Show Gist options
  • Save songzhiyong/7764355 to your computer and use it in GitHub Desktop.
Save songzhiyong/7764355 to your computer and use it in GitHub Desktop.
package net.jthoenes.blog.spike.lambda;
import java.util.Arrays;
import java.util.List;
public class LambdaIntro {
public static interface ItemWithIndexVisitor<E> {
public void visit(E item, int index);
}
public static <E> void eachWithIndex(List<E> list, ItemWithIndexVisitor<E> visitor) {
for (int i = 0; i < list.size(); i++) {
visitor.visit(list.get(i), i);
}
}
public static void main(String[] args) {
List<String> list = Arrays.asList("A", "B", "C");
eachWithIndex(list,
(value, index) -> {
String output = String.format("%d -> %s", index, value);
System.out.println(output);
}
);
eachWithIndex(list, LambdaIntro::printItem);
}
public static <E> void printItem(E value, int index) {
String output = String.format("%d -> %s", index, value);
System.out.println(output);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment