This repository was archived by the owner on Feb 10, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +54
-10
lines changed
stream-api/src/main/java/com/bobocode Expand file tree Collapse file tree 3 files changed +54
-10
lines changed Original file line number Diff line number Diff line change @@ -12,12 +12,12 @@ public class ImperativeVsDeclarativeFiltering {
12
12
public static void main (String [] args ) {
13
13
List <Account > accounts = Accounts .getAccountList (10 );
14
14
15
- findAllGmailAccountImperatively (accounts );
16
- findAllGmailAccountDeclaratively (accounts );
15
+ findAllGmailAccountsImperatively (accounts );
16
+ findAllGmailAccountsDeclaratively (accounts );
17
17
18
18
}
19
19
20
- private static List <Account > findAllGmailAccountImperatively (List <Account > accounts ) {
20
+ private static List <Account > findAllGmailAccountsImperatively (List <Account > accounts ) {
21
21
List <Account > gmailAccounts = new ArrayList <>();
22
22
for (Account account : accounts ) {
23
23
if (account .getEmail ().endsWith ("@gmail.com" )) {
@@ -27,9 +27,9 @@ private static List<Account> findAllGmailAccountImperatively(List<Account> accou
27
27
return gmailAccounts ;
28
28
}
29
29
30
- private static void findAllGmailAccountDeclaratively (List <Account > accounts ) {
31
- accounts .stream ()
32
- .filter (a -> a .getEmail ().endsWith ("@gmail" ))
30
+ private static List < Account > findAllGmailAccountsDeclaratively (List <Account > accounts ) {
31
+ return accounts .stream ()
32
+ .filter (a -> a .getEmail ().endsWith ("@gmail.com " ))
33
33
.collect (toList ());
34
34
}
35
35
Original file line number Diff line number Diff line change 5
5
import java .util .stream .LongStream ;
6
6
7
7
public class StreamParallelProcessing {
8
- static final long STREAM_SIZE = 10_0000_000 ;
8
+ static final long STREAM_SIZE = 100_000_000 ;
9
9
static final int N = 10 ;
10
10
11
11
public static void main (String [] args ) {
12
- LongPredicate isEven = n -> n % 2 == 0 ;
12
+ LongPredicate isDivisibleBySeven = n -> n % 7 == 0 ;
13
13
14
14
System .out .println ("Sequential processing" );
15
- performNTimes (N , () -> LongStream .range (1 , STREAM_SIZE ).filter (isEven ).count ());
15
+ performNTimes (N , () -> LongStream .range (1 , STREAM_SIZE )
16
+ .filter (isDivisibleBySeven )
17
+ .count ());
16
18
17
19
System .out .println ("\n Parallel processing" );
18
- performNTimes (N , () -> LongStream .range (1 , STREAM_SIZE ).parallel ().filter (isEven ).count ());
20
+ performNTimes (N , () -> LongStream .range (1 , STREAM_SIZE )
21
+ .parallel ()
22
+ .filter (isDivisibleBySeven )
23
+ .count ());
19
24
20
25
}
21
26
Original file line number Diff line number Diff line change
1
+ package com .bobocode ;
2
+
3
+ import com .bobocode .data .Accounts ;
4
+ import com .bobocode .model .Account ;
5
+
6
+ import java .util .List ;
7
+
8
+ public class StreamWhileExample {
9
+ public static void main (String [] args ) {
10
+ List <Account > accounts = Accounts .getAccountList (10 );
11
+
12
+ printAllEmails (accounts );
13
+ printAllEmailsWhileGmail (accounts );
14
+ printAllEmailsWhileNotGmail (accounts );
15
+ }
16
+
17
+ private static void printAllEmails (List <Account > accounts ) {
18
+ System .out .println ("Whole emails list:" );
19
+ accounts .stream ()
20
+ .map (Account ::getEmail )
21
+ .forEach (System .out ::println );
22
+ }
23
+
24
+ private static void printAllEmailsWhileGmail (List <Account > accounts ) {
25
+ System .out .println ("\n All while gmail:" );
26
+ accounts .stream ()
27
+ .takeWhile (a -> a .getEmail ().endsWith ("@gmail.com" ))
28
+ .map (Account ::getEmail )
29
+ .forEach (System .out ::println );
30
+ }
31
+
32
+ private static void printAllEmailsWhileNotGmail (List <Account > accounts ) {
33
+ System .out .println ("\n All while not gmail:" );
34
+ accounts .stream ()
35
+ .dropWhile (a -> a .getEmail ().endsWith ("@gmail.com" ))
36
+ .map (Account ::getEmail )
37
+ .forEach (System .out ::println );
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments