Skip to content

Commit b9830c8

Browse files
committed
chapter 5.5
1 parent 6d798f1 commit b9830c8

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.hkbea.chap5;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
public class PuttingIntoPractice {
9+
10+
public static void main(String[] args) {
11+
Trader raoul = new Trader("Raoul", "Cambridge");
12+
Trader mario = new Trader("Mario","Milan");
13+
Trader alan = new Trader("Alan","Cambridge");
14+
Trader brian = new Trader("Brian","Cambridge");
15+
16+
List<Transaction> transactions = Arrays.asList(
17+
new Transaction(brian, 2011, 300),
18+
new Transaction(raoul, 2012, 1000),
19+
new Transaction(raoul, 2011, 400),
20+
new Transaction(mario, 2012, 710),
21+
new Transaction(mario, 2012, 700),
22+
new Transaction(alan, 2012, 950)
23+
);
24+
25+
List<Transaction> tr2011 = transactions.stream().filter(t -> t.getYear() == 2011).sorted(Comparator.comparing(Transaction::getValue)).collect(Collectors.toList());
26+
System.out.println(tr2011);
27+
28+
List<String> citys = transactions.stream().map(t -> t.getTrader().getCity()).distinct().collect(Collectors.toList());
29+
System.out.println(citys);
30+
31+
List<Trader> traders = transactions.stream().map(Transaction::getTrader).distinct().filter(t -> "Cambridge".equals(t.getCity())).sorted(Comparator.comparing(Trader::getName)).collect(Collectors.toList());
32+
System.out.println(traders);
33+
34+
String traderStr = transactions.stream().map(t -> t.getTrader().getName()).distinct().sorted().reduce("", String::concat);
35+
System.out.println(traderStr);
36+
37+
System.out.println(transactions.stream().anyMatch(t -> "Milan".equals(t.getTrader().getCity())));
38+
39+
transactions.stream().filter(t -> "Cambridge".equals(t.getTrader().getCity())).mapToInt(Transaction::getValue).forEach(System.out::println);
40+
41+
transactions.stream().mapToInt(Transaction::getValue).reduce(Integer::max).ifPresent(System.out::println);
42+
43+
transactions.stream().reduce((t1, t2) -> t1.getValue() < t2.getValue() ? t1:t2).ifPresent(System.out::println);
44+
45+
}
46+
47+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.hkbea.chap5;
2+
3+
public class Trader {
4+
private String name;
5+
6+
private String city;
7+
8+
public Trader(String name, String city) {
9+
super();
10+
this.name = name;
11+
this.city = city;
12+
}
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
22+
public String getCity() {
23+
return city;
24+
}
25+
26+
public void setCity(String city) {
27+
this.city = city;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return "Trader:"+this.name + " in " + this.city;
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.hkbea.chap5;
2+
3+
public class Transaction {
4+
5+
private final Trader trader;
6+
7+
private final int year;
8+
9+
private final int value;
10+
11+
public Transaction(Trader trader, int year, int value) {
12+
super();
13+
this.trader = trader;
14+
this.year = year;
15+
this.value = value;
16+
}
17+
18+
public Trader getTrader() {
19+
return trader;
20+
}
21+
22+
public int getYear() {
23+
return year;
24+
}
25+
26+
public int getValue() {
27+
return value;
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return "{" + this.trader + ", " +
33+
"year: "+this.year+", " +
34+
"value:" + this.value +"}";
35+
}
36+
37+
}

0 commit comments

Comments
 (0)