Lambda
-------------Ex:1----------------
package com.gs.iilp.corejava.java8;
interface Flyable {
public void fly();
}
public class Lambda1 {
public static void main(String[] args) {
Flyable flyable = new Flyable() {
@Override
public void fly() {
System.out.println("Fly without lambda");
}
};
flyable.fly();
}
}
-------------Ex:2----------------
package com.gs.iilp.corejava.java8;
@FunctionalInterface
interface Flyable {
public void fly();
}
public class Lambda1 {
public static void main(String[] args) {
Flyable flyable = () -> {
System.out.println("Fly with lambda");
};
flyable.fly();
}
}
-------------Ex:3--------------------
package com.gs.iilp.corejava.java8;
@FunctionalInterface
interface Flyable {
public void fly(String name);
}
public class Lambda1 {
public static void main(String[] args) {
Flyable flyable = (name) -> {
System.out.println("Fly with lambda " + name);
};
flyable.fly("Bird");
}
}
-------------Ex:4--------------------
package com.gs.iilp.corejava.java8;
@FunctionalInterface
interface Flyable {
public void fly(String name);
}
public class Lambda1 {
public static void main(String[] args) {
Flyable flyable = name -> {
System.out.println("Fly with lambda " + name);
};
flyable.fly("Bird");
}
}
-------------Ex:5--------------------
package com.gs.iilp.corejava.java8;
@FunctionalInterface
interface Flyable {
public void fly(String name, int wings);
}
public class Lambda1 {
public static void main(String[] args) {
Flyable flyable = (n, w) -> {
System.out.println("Fly with lambda " + n + " wings are " + w);
};
flyable.fly("Bird", 2);
}
}
-------------Ex:6--------------------
package com.gs.iilp.corejava.java8;
interface Multiplyable {
public int multiply(int a, int b);
}
public class Lambda2 {
public static void main(String[] args) {
Multiplyable multiplyable = (a, b) -> (a * b);
System.out.println(multiplyable.multiply(4, 7));
}
}
-------------Ex:7--------------------
package com.gs.iilp.corejava.java8;
interface Multiplyable {
public int multiply(int a, int b);
}
public class Lambda2 {
public static void main(String[] args) {
Multiplyable multiplyable = (a, b) -> {
return a * b;
};
System.out.println(multiplyable.multiply(4, 7));
}
}
-------------Ex:8--------------------
package com.gs.iilp.corejava.java8;
interface Multiplyable {
public int multiply(int a, int b);
}
public class Lambda2 {
public static void main(String[] args) {
Multiplyable multiplyable = (int a, int b) -> {
return a * b;
};
System.out.println(multiplyable.multiply(4, 7));
}
}
-------------Ex:9--------------------
package com.gs.iilp.corejava.java8;
import java.util.ArrayList;
import java.util.List;
public class Lambda3 {
public static void main(String[] args) {
List<String> names = new ArrayList<String>();
names.add("Mohit");
names.add("Shohom");
names.add("Jatin");
names.add("Keshav");
names.forEach((s) -> {
if (s.contains("o")) {
System.out.println(s);
}
});
}
}
-------------Ex:10--------------------
package com.gs.iilp.corejava.java8;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Lambda4 {
public static void main(String[] args) {
List<String> names = new ArrayList<String>();
names.add("Mohit");
names.add("Shohom");
names.add("Jatin");
names.add("Keshav");
System.out.println("before sorting");
System.out.println(names);
System.out.println("after sorting using comparator java 7 annonyous class");
Collections.sort(names, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
System.out.println(names);
}
}
-------------Ex:11--------------------
package com.gs.iilp.corejava.java8;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Lambda4 {
public static void main(String[] args) {
List<String> names = new ArrayList<String>();
names.add("Mohit");
names.add("Shohom");
names.add("Jatin");
names.add("Keshav");
System.out.println("before sorting");
System.out.println(names);
System.out.println("after sorting using comparator java 7 annonyous class");
Collections.sort(names, (o1, o2) -> {
return o1.compareTo(o2);
});
System.out.println(names);
}
}
-------------Ex:12--------------------
package com.gs.iilp.corejava.java8;
public class Lambda5 {
public static void main(String[] args) {
new Thread(() -> {
System.out.println("Thread 1");
}).start();
new Thread(() -> {
System.out.println("Thread 2");
}).start();
}
}
Last updated