1
+ package me .ramswaroop .java8 ;
2
+
3
+ import java .util .Arrays ;
4
+ import java .util .List ;
5
+
6
+ import static java .util .Comparator .comparing ;
7
+ import static java .util .stream .Collectors .toList ;
8
+
9
+ /**
10
+ * @author ramswaroop
11
+ * @version 02/02/2017
12
+ */
13
+ class Trader {
14
+
15
+ private final String name ;
16
+ private final String city ;
17
+
18
+ public Trader (String n , String c ) {
19
+ this .name = n ;
20
+ this .city = c ;
21
+ }
22
+
23
+ public String getName () {
24
+ return this .name ;
25
+ }
26
+
27
+ public String getCity () {
28
+ return this .city ;
29
+ }
30
+
31
+ public String toString () {
32
+ return "Trader:" + this .name + " in " + this .city ;
33
+ }
34
+ }
35
+
36
+ class Transaction {
37
+ private final Trader trader ;
38
+ private final int year ;
39
+ private final int value ;
40
+
41
+ public Transaction (Trader trader , int year , int value ) {
42
+ this .trader = trader ;
43
+ this .year = year ;
44
+ this .value = value ;
45
+ }
46
+
47
+ public Trader getTrader () {
48
+ return this .trader ;
49
+ }
50
+
51
+ public int getYear () {
52
+ return this .year ;
53
+ }
54
+
55
+ public int getValue () {
56
+ return this .value ;
57
+ }
58
+
59
+ public String toString () {
60
+ return "{" + this .trader + ", " +
61
+ "year: " + this .year + ", " +
62
+ "value:" + this .value + "}" ;
63
+ }
64
+ }
65
+
66
+ public class Streams {
67
+
68
+ static final Trader raoul = new Trader ("Raoul" , "Cambridge" );
69
+ static final Trader mario = new Trader ("Mario" , "Milan" );
70
+ static final Trader alan = new Trader ("Alan" , "Cambridge" );
71
+ static final Trader brian = new Trader ("Brian" , "Cambridge" );
72
+
73
+ static final List <Transaction > transactions = Arrays .asList (
74
+ new Transaction (brian , 2011 , 300 ),
75
+ new Transaction (raoul , 2012 , 1000 ),
76
+ new Transaction (raoul , 2011 , 400 ),
77
+ new Transaction (mario , 2012 , 710 ),
78
+ new Transaction (mario , 2012 , 700 ),
79
+ new Transaction (alan , 2012 , 950 )
80
+ );
81
+
82
+ public static List <Transaction > getTransactionsIn2011SortedByValue () {
83
+ return transactions .stream ()
84
+ .filter (t -> t .getYear () == 2011 )
85
+ .sorted (comparing (Transaction ::getValue ))
86
+ .collect (toList ());
87
+ }
88
+
89
+ public static List <String > findUniqueCities () {
90
+ return transactions .stream ()
91
+ .map (t -> t .getTrader ().getCity ())
92
+ .distinct ()
93
+ .collect (toList ());
94
+ }
95
+
96
+ public static List <Trader > getAllTradersFromCambridgeAndSortByName () {
97
+ return transactions .stream ()
98
+ .map (Transaction ::getTrader )
99
+ .filter (traders -> traders .getCity ().equals ("Cambridge" ))
100
+ .distinct ()
101
+ .sorted (comparing (Trader ::getName ))
102
+ .collect (toList ());
103
+ }
104
+
105
+ public static List <String > getAllTraderNamesAndSortByName () {
106
+ return transactions .stream ()
107
+ .map (t -> t .getTrader ().getName ())
108
+ .distinct ()
109
+ .sorted ()
110
+ .collect (toList ());
111
+ }
112
+
113
+ public static boolean areAnyTradersFromMilan () {
114
+ return transactions .stream ()
115
+ .anyMatch (t -> t .getTrader ().getCity ().equals ("Milan" ));
116
+ }
117
+
118
+ public static void main (String [] a ) {
119
+ System .out .println (getTransactionsIn2011SortedByValue ());
120
+ System .out .println (findUniqueCities ());
121
+ System .out .println (getAllTradersFromCambridgeAndSortByName ());
122
+ System .out .println (getAllTraderNamesAndSortByName ());
123
+ System .out .println (areAnyTradersFromMilan ());
124
+ }
125
+ }
0 commit comments