Skip to content
This repository was archived by the owner on Feb 10, 2021. It is now read-only.

Commit 3692332

Browse files
author
Taras
committed
- renamed examples according the convention
- added couple examples like ImperativeVsDeclarativeFiltering.java, and MethodRefToUpperCaseExample.java
1 parent c888311 commit 3692332

20 files changed

+204
-48
lines changed

lambdas/src/main/java/com/bobocode/MethodReference.java renamed to lambdas/src/main/java/com/bobocode/LambdaAndMethodReference.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* Method reference is a shorthand for lambda expression that could be use in some cases for better readability.
1111
*/
12-
public class MethodReference {
12+
public class LambdaAndMethodReference {
1313
public static void main(String[] args) {
1414
List<Account> accounts = Accounts.getAccountList(10);
1515

lambdas/src/main/java/com/bobocode/ComparatorExample.java renamed to lambdas/src/main/java/com/bobocode/LambdaComparatorExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import static java.util.Comparator.comparing;
1010

11-
public class ComparatorExample {
11+
public class LambdaComparatorExample {
1212

1313
public static void main(String[] args) {
1414
List<Account> accounts = Accounts.getAccountList(10);

lambdas/src/main/java/com/bobocode/RunnableExample.java renamed to lambdas/src/main/java/com/bobocode/LambdaRunnableExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.bobocode;
22

3-
public class RunnableExample {
3+
public class LambdaRunnableExample {
44
public static void main(String[] args) {
55
sayHelloFromNewThread();
66
sayHelloFromNewThreadUsingLambda();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.bobocode;
2+
3+
import java.util.function.Function;
4+
import java.util.function.Supplier;
5+
import java.util.function.UnaryOperator;
6+
7+
/**
8+
* A list of different examples of method reference usage.
9+
*/
10+
public class MethodRefToUpperCaseExample {
11+
public static void main(String[] args) {
12+
String s = "Method reference insights";
13+
14+
printUpperStringUsingFunction(s);
15+
printUpperStringUsingUnaryOperation(s);
16+
printUpperStringUsingStringSupplier(s);
17+
}
18+
19+
/**
20+
* This is the unbound method reference example. Since we used unbound method reference, the {@link Function} can
21+
* receive a string as an input parameter.
22+
*/
23+
private static void printUpperStringUsingFunction(String s) {
24+
Function<String, String> upperCaseFunction = String::toUpperCase;
25+
26+
System.out.println(upperCaseFunction.apply(s));
27+
}
28+
29+
/**
30+
* This is the unbound method reference example. Since toUpperCase() receives and returns the same type
31+
* {@link String}, we can easily replace {@link Function} with {@link UnaryOperator}
32+
*/
33+
private static void printUpperStringUsingUnaryOperation(String s) {
34+
UnaryOperator<String> upperCaseOperator = String::toUpperCase;
35+
36+
System.out.println(upperCaseOperator.apply(s));
37+
}
38+
39+
/**
40+
* This is the bound method reference example. We actually reference a method that is bound to concrete
41+
* string instance. Since we reference a concrete string, there is no input parameter, only an output. In this case
42+
* we can use {@link Supplier}
43+
*/
44+
private static void printUpperStringUsingStringSupplier(String s) {
45+
Supplier<String> stringSupplier = s::toUpperCase;
46+
47+
System.out.println(stringSupplier.get());
48+
}
49+
50+
}
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.bobocode;
22

3-
import java.util.function.BiFunction;
4-
import java.util.function.Function;
3+
import java.util.function.*;
54

65
/**
76
* A list of method reference examples.
@@ -10,21 +9,27 @@ public class MethodReferenceExamples {
109
public static void main(String[] args) {
1110
printAbsUsingMethodReference(-23);
1211
printSumUsingMethodReference(25, 50);
12+
printUpperStringUsingMethodReference("Lambda is awesome!");
1313
}
1414

1515
private static void printAbsUsingMethodReference(int a) {
16-
Function<Integer, Integer> absFunction = Math::abs;
17-
int result = absFunction.apply(a);
16+
IntUnaryOperator absOperator = Math::abs;
17+
int result = absOperator.applyAsInt(a);
1818

1919
System.out.println("abd(" + a + ") = " + result);
2020
}
2121

2222
private static void printSumUsingMethodReference(int a, int b) {
23-
BiFunction<Integer, Integer, Integer> sumFunction = Math::addExact;
24-
int result = sumFunction.apply(a, b);
23+
IntBinaryOperator sumOperator = Math::addExact;
24+
int result = sumOperator.applyAsInt(a, b);
2525

26-
System.out.println(a +" + "+ b + " = " + result);
26+
System.out.println("\n" + a + " + " + b + " = " + result);
2727
}
2828

29+
private static void printUpperStringUsingMethodReference(String s) {
30+
UnaryOperator<String> upperOperation = String::toUpperCase;
31+
32+
System.out.println("\n" + s + " -> " + upperOperation.apply(s));
33+
}
2934

3035
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.bobocode;
2+
3+
import java.util.Random;
4+
import java.util.function.DoubleSupplier;
5+
import java.util.function.IntToDoubleFunction;
6+
import java.util.function.LongBinaryOperator;
7+
8+
/**
9+
* Always use special classes for Primitives
10+
*/
11+
public class PredefinedInterfacePrimitives {
12+
public static void main(String[] args) {
13+
printRandomDoubleUsingSupplier();
14+
printLongSumUsingBinaryOperation(124124L, 132134L);
15+
printSqrtUsingFunction(25);
16+
}
17+
18+
private static void printRandomDoubleUsingSupplier() {
19+
DoubleSupplier doubleSupplier = () -> new Random().nextDouble();
20+
21+
System.out.println("Random double: " + doubleSupplier.getAsDouble());
22+
}
23+
24+
private static void printLongSumUsingBinaryOperation(long a, long b) {
25+
LongBinaryOperator sumOperator = Long::sum;
26+
27+
System.out.println("\n" + a + " " + b + " = " + sumOperator.applyAsLong(a, b));
28+
}
29+
30+
private static void printSqrtUsingFunction(int a) {
31+
IntToDoubleFunction sqrtFunction = Math::sqrt;
32+
33+
System.out.println("\nsqrt(" + a + ") = " + sqrtFunction.applyAsDouble(a));
34+
}
35+
36+
}

lambdas/src/main/java/com/bobocode/PredefinedInterfacesExamples.java

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,61 @@
44
import com.bobocode.model.Account;
55

66
import java.util.Random;
7-
import java.util.function.Consumer;
8-
import java.util.function.Function;
9-
import java.util.function.Predicate;
10-
import java.util.function.Supplier;
7+
import java.util.function.*;
118
import java.util.stream.IntStream;
129

1310
/**
1411
* A list of predefined interfaces examples.
1512
*/
1613
public class PredefinedInterfacesExamples {
1714
public static void main(String[] args) {
18-
1915
printEmailUsingAccountConsumer();
20-
2116
printARandomNumberUsingIntegerSupplier();
22-
2317
calculate3xValueUsingIntegerFunction();
24-
2518
checkIfNumberIsPositiveUsingIntegerPredicate();
26-
2719
verifyGoogleEmailUsingAccountPredicate();
28-
2920
printPrimeNumbersUsingIntegerPredicate();
30-
3121
}
3222

3323
private static void printARandomNumberUsingIntegerSupplier() {
34-
Supplier<Integer> integerSupplier = () -> new Random().nextInt(1000);
24+
IntSupplier integerSupplier = () -> new Random().nextInt(1000);
3525

36-
System.out.println("Next value: " + integerSupplier.get());
26+
System.out.println("\nNext random value: " + integerSupplier.getAsInt());
3727
}
3828

3929
private static void printEmailUsingAccountConsumer() {
40-
Consumer<Account> accountConsumer = acc -> System.out.println(acc.getEmail());
30+
Consumer<Account> accountConsumer = acc -> System.out.println("\nAccount email: " + acc.getEmail());
4131
Account account = Accounts.getAccount();
4232

4333
accountConsumer.accept(account);
4434
}
4535

4636
private static void calculate3xValueUsingIntegerFunction() {
47-
Function<Integer, Integer> tripleFunction = n -> 3 * n;
37+
IntUnaryOperator tripleFunction = n -> 3 * n;
4838
int a = 12;
4939

50-
System.out.println("3 * " + a + tripleFunction.apply(a));
40+
System.out.println("\n3 * " + a + " = " + tripleFunction.applyAsInt(a));
5141
}
5242

5343
private static void checkIfNumberIsPositiveUsingIntegerPredicate() {
54-
Predicate<Integer> isPositive = n -> n > 0;
55-
int b = 23;
44+
IntPredicate isPositive = n -> n > 0;
45+
int b = new Random().nextInt();
5646

57-
System.out.println(b + " is " + (isPositive.test(b) ? "positive" : "negative"));
47+
System.out.println("\n" + b + " is " + (isPositive.test(b) ? "positive" : "negative"));
5848
}
5949

6050
private static void verifyGoogleEmailUsingAccountPredicate() {
6151
Account account = Accounts.getAccount();
6252
Predicate<Account> isGmailUser = a -> a.getEmail().endsWith("@gmail.com");
6353

64-
System.out.println("\"" + account.getEmail() + "\"" + " is "
65-
+ (isGmailUser.test(account) ? "" : "not") + " a Google mail.");
54+
System.out.println("\n" + account.getEmail() + " is "
55+
+ (isGmailUser.test(account) ? "" : "not") + " a Google email.");
6656
}
6757

6858
private static void printPrimeNumbersUsingIntegerPredicate() {
69-
Predicate<Integer> isPrime = n -> IntStream.range(2, n).noneMatch(i -> n % i == 0);
70-
71-
IntStream.range(1, 10)
59+
IntPredicate isPrime = n -> IntStream.range(2, n).noneMatch(i -> n % i == 0);
60+
System.out.println();
61+
IntStream.rangeClosed(1, 25)
7262
.forEach(i -> System.out.printf("%3d %10s\n", i, (isPrime.test(i) ? " is prime" : "")));
7363
}
7464
}

optional/src/main/java/com/bobocode/OptionalMappingExample.java renamed to optional/src/main/java/com/bobocode/OptionalMapping.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.List;
1111
import java.util.Optional;
1212

13-
public class OptionalMappingExample {
13+
public class OptionalMapping {
1414
public static void main(String[] args) {
1515
List<Account> accounts = Accounts.getAccountList(10);
1616
List<CreditAccount> creditAccounts = Collections.singletonList(new CreditAccount(BigDecimal.valueOf(1000)));

optional/src/main/java/com/bobocode/OptionalForPrimitivesExample.java renamed to optional/src/main/java/com/bobocode/OptionalPrimitives.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.util.List;
88
import java.util.OptionalDouble;
99

10-
public class OptionalForPrimitivesExample {
10+
public class OptionalPrimitives {
1111
public static void main(String[] args) {
1212
List<Account> accounts = Accounts.getAccountList(10);
1313

optional/src/main/java/com/bobocode/OptionalReturningMethodExample.java renamed to optional/src/main/java/com/bobocode/OptionalReturningMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.util.Optional;
88
import java.util.Random;
99

10-
public class OptionalReturningMethodExample {
10+
public class OptionalReturningMethod {
1111
public static void main(String[] args) {
1212
List<Account> accounts = Accounts.getAccountList(20);
1313
Optional<Account> luckyGuy = findLuckyGuy(accounts);

0 commit comments

Comments
 (0)