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

Commit df8379e

Browse files
author
Taras
committed
- implemented predefined interfaces task
- added stream collect() examples
1 parent b4d2227 commit df8379e

File tree

5 files changed

+33
-54
lines changed

5 files changed

+33
-54
lines changed

lambda-basics/src/main/java/com/bobocode/LambdaTask_PredefinedInterfaces.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.bobocode.model.Account;
44

55
import java.util.function.Predicate;
6+
import java.util.stream.IntStream;
67

78
/**
89
* Here you can find a list of various predefined interface variables. Your job is to provide implementation foe each
@@ -14,10 +15,16 @@ public static void main(String[] args) {
1415
// Predicates. As you remember Predicate<T> is a functional interface, its method test() receives one argument
1516
// with type T and returns boolean
1617

17-
Predicate<Integer> isNegative; // check whether a value is less than zero
18-
Predicate<Account> useGoogle; // check whether its email ends with gmail.com
19-
Predicate<Integer> isPrime; // implement logic that checks whether a number is prime
18+
// check whether a value is less than zero
19+
Predicate<Integer> isNegative = n -> n < 0;
20+
// check whether its email ends with gmail.com
21+
Predicate<Account> useGoogle = a -> a.getEmail().endsWith("gmail.com");
2022

23+
// implement logic that checks whether a number is prime
24+
Predicate<Integer> isPrime = n -> IntStream.range(2, n).noneMatch(i -> n % i == 0);
25+
26+
IntStream.range(1, 50)
27+
.forEach(i -> System.out.printf("%3d %10s\n", i, (isPrime.test(i) ? " is prime":"")));
2128

2229
}
2330

optional-basics/src/main/java/com/bobocode/util/TestDataProvider.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

stream-api/src/main/java/com/bobocode/StreamExamples_AdditionalFeatures.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class StreamExamples_AdditionalFeatures {
1919
public static void main(String[] args) {
2020
List<Account> accounts = TestDataProvider.generateAccountList();
2121

22-
// Soring elements
22+
// Sorting elements
2323
System.out.println("Account owner's sorter names: ");
2424
accounts.stream()
2525
.sorted(comparing(Account::getFirstName))

stream-api/src/main/java/com/bobocode/StreamExamples_Collecting.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
import com.bobocode.util.TestDataProvider;
66

77
import java.time.Month;
8+
import java.util.Comparator;
89
import java.util.List;
910
import java.util.Map;
11+
import java.util.Optional;
1012

13+
import static java.util.Comparator.*;
1114
import static java.util.stream.Collectors.*;
1215

1316
/**
@@ -56,5 +59,24 @@ public static void main(String[] args) {
5659
.map(Account::getFirstName)
5760
.collect(joining(", "));
5861
System.out.println(concatenatedName);
62+
63+
64+
Map<Boolean, List<Account>> accountByNameLength = accounts.stream()
65+
.collect(partitioningBy(a -> a.getFirstName().length() > 4));
66+
67+
Map<Boolean, Optional<Account>> lengthNameAccountsWithMaxBalance = accounts.stream()
68+
.collect(
69+
partitioningBy(a -> a.getFirstName().length() > 4,
70+
maxBy(comparing(Account::getBalance)))
71+
);
72+
73+
74+
Map<Boolean, Map<Month, List<String>>> accountByNameLengthByBirthdayMonth = accounts.stream()
75+
.collect(partitioningBy(a -> a.getFirstName().length() > 4,
76+
groupingBy(a -> a.getBirthday().getMonth(),
77+
mapping(Account::getFirstName, toList()))));
78+
79+
System.out.println(accountByNameLengthByBirthdayMonth);
80+
5981
}
6082
}

stream-api/src/main/java/com/bobocode/model/Account.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)