Skip to content

Instantly share code, notes, and snippets.

@vedraiyani
Created July 1, 2021 05:30
Show Gist options
  • Save vedraiyani/4ec28e4c87f0a9abd779c1341592a8d6 to your computer and use it in GitHub Desktop.
Save vedraiyani/4ec28e4c87f0a9abd779c1341592a8d6 to your computer and use it in GitHub Desktop.
Understand how lambda works in java
import java.util.Arrays;
import java.util.function.Function;
import java.util.function.IntConsumer;
public class HelloWorld{
// https://blogs.oracle.com/javamagazine/behind-the-scenes-how-do-lambda-expressions-really-work-in-java
// javac .\HelloWorld.java
// javap -p -c HelloWorld$1
String sum="";
public static void main(String []args){
new HelloWorld().main(); //comment then uncomment below comments also
} //comment
public void main(){ // comment
System.out.println("Hello World");
int[] values={1,2,3};
Arrays.stream(values).forEach(a->{
// sum+=a;
System.out.println(sum + a);
});
Arrays.stream(values).forEach(a->{
// sum+=a;
System.out.println("sum2");
});
Arrays.stream(values).forEach(new IntConsumer(){
@Override
public void accept(int t) {
System.out.println("sum3");
}
});
Arrays.stream(values).forEach(new IntConsumer(){
@Override
public void accept(int t) {
sum+=" hi";
System.out.println(sum);
System.out.println("sum3");
}
});
// Arrays.stream(values).forEach((IntConsumer) new Function<Integer,Void>(){
// @Override
// public Void apply(Integer t) {
// System.out.println(sum);
// return null;
// }
// });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment