forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreams.java
153 lines (126 loc) · 4.56 KB
/
Streams.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.rampatra.java8;
import java.util.Arrays;
import java.util.List;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
/**
* @author rampatra
* @version 02/02/2017
*/
class Trader {
private final String name;
private final String city;
public Trader(String n, String c) {
this.name = n;
this.city = c;
}
public String getName() {
return this.name;
}
public String getCity() {
return this.city;
}
public String toString() {
return "Trader:" + this.name + " in " + this.city;
}
}
class Transaction {
private final Trader trader;
private final int year;
private final int value;
public Transaction(Trader trader, int year, int value) {
this.trader = trader;
this.year = year;
this.value = value;
}
public Trader getTrader() {
return this.trader;
}
public int getYear() {
return this.year;
}
public int getValue() {
return this.value;
}
public String toString() {
return "{" + this.trader + ", " +
"year: " + this.year + ", " +
"value:" + this.value + "}";
}
}
public class Streams {
static final Trader raoul = new Trader("Raoul", "Cambridge");
static final Trader mario = new Trader("Mario", "Milan");
static final Trader alan = new Trader("Alan", "Cambridge");
static final Trader brian = new Trader("Brian", "Cambridge");
static final List<Transaction> transactions = Arrays.asList(
new Transaction(brian, 2011, 300),
new Transaction(raoul, 2012, 1000),
new Transaction(raoul, 2011, 400),
new Transaction(mario, 2012, 710),
new Transaction(mario, 2012, 700),
new Transaction(alan, 2012, 950)
);
public static List<Transaction> getTransactionsIn2011SortedByValue() {
return transactions.stream()
.filter(t -> t.getYear() == 2011)
.sorted(comparing(Transaction::getValue))
.collect(toList());
}
public static List<String> findUniqueCities() {
return transactions.stream()
.map(t -> t.getTrader().getCity())
.distinct()
.collect(toList());
}
public static List<Trader> getAllTradersFromCambridgeAndSortByName() {
return transactions.stream()
.map(Transaction::getTrader)
.filter(traders -> traders.getCity().equals("Cambridge"))
.distinct()
.sorted(comparing(Trader::getName))
.collect(toList());
}
public static List<String> getAllTraderNamesAndSortByName() {
return transactions.stream()
.map(t -> t.getTrader().getName())
.distinct()
.sorted()
.collect(toList());
}
public static boolean areAnyTradersFromMilan() {
return transactions.stream()
.anyMatch(t -> "Milan".equals(t.getTrader().getCity()));
}
public static Integer[] getAllTransValuesFromTradersInCambridge() {
return transactions.stream()
.filter(t -> "Cambridge".equals(t.getTrader().getCity()))
.map(Transaction::getValue)
.toArray(Integer[]::new);
}
public static int findHighestTransactionValue() {
return transactions.stream()
.mapToInt(Transaction::getValue)
.max().getAsInt();
/* this is another solution
return transactions.stream()
.map(Transaction::getValue)
.reduce((t1, t2) -> (t1 > t2) ? t1 : t2) // you can replace with .reduce(Integer::max)
.get();*/
}
public static Transaction getSmallestTransaction() {
return transactions.stream()
.reduce((t1, t2) -> t1.getValue() < t2.getValue() ? t1 : t2)
.get();
}
public static void main(String[] args) {
System.out.println("1: " + getTransactionsIn2011SortedByValue());
System.out.println("2: " + findUniqueCities());
System.out.println("3: " + getAllTradersFromCambridgeAndSortByName());
System.out.println("4: " + getAllTraderNamesAndSortByName());
System.out.println("5: " + areAnyTradersFromMilan());
System.out.println("6: " + Arrays.asList(getAllTransValuesFromTradersInCambridge()));
System.out.println("7: " + findHighestTransactionValue());
System.out.println("8: " + getSmallestTransaction());
}
}