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 {
1212 public static void main (String [] args ) {
1313 List <Account > accounts = Accounts .getAccountList (10 );
1414
15- findAllGmailAccountImperatively (accounts );
16- findAllGmailAccountDeclaratively (accounts );
15+ findAllGmailAccountsImperatively (accounts );
16+ findAllGmailAccountsDeclaratively (accounts );
1717
1818 }
1919
20- private static List <Account > findAllGmailAccountImperatively (List <Account > accounts ) {
20+ private static List <Account > findAllGmailAccountsImperatively (List <Account > accounts ) {
2121 List <Account > gmailAccounts = new ArrayList <>();
2222 for (Account account : accounts ) {
2323 if (account .getEmail ().endsWith ("@gmail.com" )) {
@@ -27,9 +27,9 @@ private static List<Account> findAllGmailAccountImperatively(List<Account> accou
2727 return gmailAccounts ;
2828 }
2929
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 " ))
3333 .collect (toList ());
3434 }
3535
Original file line number Diff line number Diff line change 55import java .util .stream .LongStream ;
66
77public class StreamParallelProcessing {
8- static final long STREAM_SIZE = 10_0000_000 ;
8+ static final long STREAM_SIZE = 100_000_000 ;
99 static final int N = 10 ;
1010
1111 public static void main (String [] args ) {
12- LongPredicate isEven = n -> n % 2 == 0 ;
12+ LongPredicate isDivisibleBySeven = n -> n % 7 == 0 ;
1313
1414 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 ());
1618
1719 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 ());
1924
2025 }
2126
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