Skip to content

Instantly share code, notes, and snippets.

@oscarryz
Last active October 26, 2016 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oscarryz/3ca018ab84d82641b9c6cd76fffc9c28 to your computer and use it in GitHub Desktop.
Save oscarryz/3ca018ab84d82641b9c6cd76fffc9c28 to your computer and use it in GitHub Desktop.
import java.util.function.BiConsumer;
import java.util.Arrays;
import java.util.List;
class SimpleClass {
void methodA(String a, String b) {
System.out.printf("%s %s%n", a, b);
}
}
class B {
void different(String x, String y) {
System.out.println(
new StringBuilder(y).append(x).reverse().toString()
);
}
}
class Service {
void doService(List<BiConsumer<String,String>> methods ) {
// this is equivalent to the 'for' below
methods.forEach( item -> item.accept("hello", "world"));
//for( BiConsumer<String,String> item : methods ) {
// item.accept("Hello", "world");
//}
}
}
class Main {
public static void main( String ... args ) {
SimpleClass sc = new SimpleClass();
B b = new B();
Service s = new Service();
List<BiConsumer<String,String>> collection = Arrays.asList(
sc::methodA,
b::different
);
s.doService( collection );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment