Skip to content

Commit e1828ac

Browse files
committed
Lambda expressions
1 parent 484ea5b commit e1828ac

10 files changed

Lines changed: 135 additions & 0 deletions
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import java.util.*;
2+
3+
public class LambdaExercise {
4+
public static void main(String[] args) {
5+
String[] array = {"Java", "C++", "Ruby", "Python"};
6+
System.out.println("Unsorted: " + Arrays.toString(array));
7+
Arrays.sort(array, (x, y) -> y.compareTo(x));
8+
System.out.println("Reverse sorted: " + Arrays.toString(array));
9+
}
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class LambdaRuleFive {
2+
public static void main(String[] args) {
3+
Printable printable = (x, y) -> System.out.println(x + " : " + y);
4+
doStuff(printable);
5+
}
6+
7+
private static void doStuff(Printable printer) {
8+
printer.print("Hello", 5);
9+
}
10+
}
11+
12+
interface Printable {
13+
public void print(String msg, int count);
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class LambdaRuleFour {
2+
public static void main(String[] args) {
3+
Printable printable = x -> {System.out.println(x);};
4+
doStuff(printable);
5+
}
6+
7+
private static void doStuff(Printable printer) {
8+
printer.print("Hello");
9+
}
10+
}
11+
12+
interface Printable {
13+
public void print(String msg);
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class LambdaRuleOne {
2+
public static void main(String[] args) {
3+
Printable printable = (String x, int y) -> {
4+
for(int i = 1; i <= y; i++) {
5+
System.out.println(x);
6+
}
7+
};
8+
doStuff(printable);
9+
}
10+
11+
private static void doStuff(Printable printer) {
12+
printer.print("Hello", 5);
13+
}
14+
}
15+
16+
interface Printable {
17+
public void print(String msg, int count);
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class LambdaRuleSix {
2+
public static void main(String[] args) {
3+
TaxCalculator calculator = x -> x * 0.02;
4+
doStuff(calculator);
5+
}
6+
7+
private static void doStuff(TaxCalculator calc) {
8+
double tax = calc.calculate(100.00);
9+
System.out.println("TAX: " + tax);
10+
}
11+
}
12+
13+
interface TaxCalculator {
14+
public double calculate(double amount);
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class LambdaRuleThree {
2+
public static void main(String[] args) {
3+
Printable printable = () -> {System.out.println("Hello world");};
4+
doStuff(printable);
5+
}
6+
7+
private static void doStuff(Printable printer) {
8+
printer.print();
9+
}
10+
}
11+
12+
interface Printable {
13+
public void print();
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class LambdaRuleTwo {
2+
public static void main(String[] args) {
3+
Printable printable = (x, y) -> {
4+
for(int i = 1; i <= y; i++) {
5+
System.out.println(x);
6+
}
7+
};
8+
doStuff(printable);
9+
}
10+
11+
private static void doStuff(Printable printer) {
12+
printer.print("Hello", 5);
13+
}
14+
}
15+
16+
interface Printable {
17+
public void print(String msg, int count);
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class LambdaThreadDemo {
2+
public static void main(String[] args) {
3+
// Runnable anonymous object
4+
Runnable runnable = () -> System.out.println("ID: " + Thread.currentThread().getId());
5+
Thread thread = new Thread(runnable);
6+
thread.start();
7+
}
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class LambdaVariableScope {
2+
public static void main(String[] args) {
3+
String name = "Java Helps";
4+
Thread validLambda = new Thread(() -> System.out.println(name));
5+
/*Thread invalidLambda = new Thread(() -> {
6+
name = "Something else"; // Cannot change the value
7+
System.out.println(name);
8+
});*/
9+
validLambda.start();
10+
}
11+
}

lambdaexpression/ThreadDemo.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class ThreadDemo {
2+
public static void main(String[] args) {
3+
// Runnable anonymous object
4+
Runnable runnable = new Runnable() {
5+
@Override
6+
public void run() {
7+
System.out.println("ID: " + Thread.currentThread().getId());
8+
}
9+
};
10+
Thread thread = new Thread(runnable);
11+
thread.start();
12+
}
13+
}

0 commit comments

Comments
 (0)