Skip to content

Commit 20b1589

Browse files
committed
Adding the source code examples for chapter 4 about Streams
1 parent 0446757 commit 20b1589

File tree

10 files changed

+372
-0
lines changed

10 files changed

+372
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package lambdasinaction.chap4;
2+
import java.util.*;
3+
4+
public class Dish {
5+
6+
private final String name;
7+
private final boolean vegetarian;
8+
private final int calories;
9+
private final Type type;
10+
11+
public Dish(String name, boolean vegetarian, int calories, Type type) {
12+
this.name = name;
13+
this.vegetarian = vegetarian;
14+
this.calories = calories;
15+
this.type = type;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public boolean isVegetarian() {
23+
return vegetarian;
24+
}
25+
26+
public int getCalories() {
27+
return calories;
28+
}
29+
30+
public Type getType() {
31+
return type;
32+
}
33+
34+
public enum Type { MEAT, FISH, OTHER }
35+
36+
@Override
37+
public String toString() {
38+
return name;
39+
}
40+
41+
public static final List<Dish> menu =
42+
Arrays.asList( new Dish("pork", false, 800, Dish.Type.MEAT),
43+
new Dish("beef", false, 700, Dish.Type.MEAT),
44+
new Dish("chicken", false, 400, Dish.Type.MEAT),
45+
new Dish("french fries", true, 530, Dish.Type.OTHER),
46+
new Dish("rice", true, 350, Dish.Type.OTHER),
47+
new Dish("season fruit", true, 120, Dish.Type.OTHER),
48+
new Dish("pizza", true, 550, Dish.Type.OTHER),
49+
new Dish("prawns", false, 400, Dish.Type.FISH),
50+
new Dish("salmon", false, 450, Dish.Type.FISH));
51+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package lambdasinaction.chap4;
2+
import java.util.stream.*;
3+
import java.util.*;
4+
5+
import static lambdasinaction.chap4.Dish.menu;
6+
7+
public class Filtering{
8+
9+
public static void main(String...args){
10+
11+
List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4);
12+
numbers.stream()
13+
.filter(i -> i % 2 == 0)
14+
.distinct()
15+
.forEach(System.out::println);
16+
17+
numbers.stream().limit(20).forEach(System.out::println);
18+
19+
}
20+
21+
22+
23+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package lambdasinaction.chap4;
2+
import java.util.stream.*;
3+
import java.util.*;
4+
5+
import static lambdasinaction.chap4.Dish.menu;
6+
7+
public class Finding{
8+
9+
public static void main(String...args){
10+
11+
if(isVegetarianFriendlyMenu()){
12+
System.out.println("Vegetarian friendly");
13+
}
14+
15+
System.out.println(isHealthyMenu());
16+
System.out.println(isHealthyMenu2());
17+
18+
Optional<Dish> dish = findVegetarianDish();
19+
dish.ifPresent(d -> System.out.println(d.getName()));
20+
21+
}
22+
23+
private static boolean isVegetarianFriendlyMenu(){
24+
return menu.stream().anyMatch(Dish::isVegetarian);
25+
}
26+
27+
private static boolean isHealthyMenu(){
28+
return menu.stream().allMatch(d -> d.getCalories() < 1000);
29+
}
30+
31+
private static boolean isHealthyMenu2(){
32+
return menu.stream().noneMatch(d -> d.getCalories() >= 1000);
33+
}
34+
35+
private static Optional<Dish> findVegetarianDish(){
36+
return menu.stream().filter(Dish::isVegetarian).findAny();
37+
}
38+
39+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package lambdasinaction.chap4;
2+
import java.util.stream.*;
3+
import java.util.*;
4+
import static java.util.stream.Collectors.toList;
5+
import static lambdasinaction.chap4.Dish.menu;
6+
7+
public class Mapping{
8+
9+
public static void main(String...args){
10+
11+
// map
12+
List<String> dishNames = menu.stream()
13+
.map(Dish::getName)
14+
.collect(toList());
15+
System.out.println(dishNames);
16+
17+
18+
// map
19+
List<String> words = Arrays.asList("Hello", "World");
20+
List<Integer> wordLengths = words.stream()
21+
.map(String::length)
22+
.collect(toList());
23+
System.out.println(wordLengths);
24+
25+
// flatMap
26+
words.stream()
27+
.flatMap((String line) -> Arrays.stream(line.split("")))
28+
.distinct()
29+
.forEach(System.out::println);
30+
31+
// flatMap
32+
List<Integer> numbers1 = Arrays.asList(1,2,3,4,5);
33+
List<Integer> numbers2 = Arrays.asList(6,7,8);
34+
List<int[]> pairs =
35+
numbers1.stream()
36+
.flatMap(i -> numbers2.stream()
37+
.map(j -> new int[]{i, j})
38+
)
39+
.filter(pair -> (pair[0] + pair[1]) % 3 == 0)
40+
.collect(toList());
41+
pairs.forEach(e -> System.out.println("(" + e[0] + ", " + e[1] + ")"));
42+
43+
}
44+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package lambdasinaction.chap4;
2+
3+
import java.util.*;
4+
import java.util.stream.*;
5+
import static java.util.Comparator.comparing;
6+
import static java.util.stream.Collectors.toList;
7+
8+
public class PuttingIntoPractice{
9+
public static void main(String ...args){
10+
Trader raoul = new Trader("Raoul", "Cambridge");
11+
Trader mario = new Trader("Mario","Milan");
12+
Trader alan = new Trader("Alan","Cambridge");
13+
Trader brian = new Trader("Brian","Cambridge");
14+
15+
List<Transaction> transactions = Arrays.asList(
16+
new Transaction(brian, 2011, 300),
17+
new Transaction(raoul, 2012, 1000),
18+
new Transaction(raoul, 2011, 400),
19+
new Transaction(mario, 2012, 710),
20+
new Transaction(mario, 2012, 700),
21+
new Transaction(alan, 2012, 950)
22+
);
23+
24+
25+
// Query 1: Find all transactions from year 2011 and sort them by value (small to high).
26+
List<Transaction> tr2011 = transactions.stream()
27+
.filter(transaction -> transaction.getYear() == 2011)
28+
.sorted(comparing(Transaction::getValue))
29+
.collect(toList());
30+
System.out.println(tr2011);
31+
32+
// Query 2: What are all the unique cities where the traders work?
33+
List<String> cities =
34+
transactions.stream()
35+
.map(transaction -> transaction.getTrader().getCity())
36+
.distinct()
37+
.collect(toList());
38+
System.out.println(cities);
39+
40+
// Query 3: Find all traders from Cambridge and sort them by name.
41+
42+
List<Trader> traders =
43+
transactions.stream()
44+
.map(Transaction::getTrader)
45+
.filter(trader -> trader.getCity().equals("Cambridge"))
46+
.distinct()
47+
.sorted(comparing(Trader::getName))
48+
.collect(toList());
49+
System.out.println(traders);
50+
51+
52+
// Query 4: Return a string of all traders’ names sorted alphabetically.
53+
54+
String traderStr =
55+
transactions.stream()
56+
.map(transaction -> transaction.getTrader().getName())
57+
.distinct()
58+
.sorted()
59+
.reduce("", (n1, n2) -> n1 + n2);
60+
System.out.println(traderStr);
61+
62+
// Query 5: Are there any trader based in Milan?
63+
64+
boolean milanBased =
65+
transactions.stream()
66+
.anyMatch(transaction -> transaction.getTrader()
67+
.getCity()
68+
.equals("Milan")
69+
);
70+
System.out.println(milanBased);
71+
72+
73+
// Query 6: Update all transactions so that the traders from Milan are set to Cambridge.
74+
transactions.stream()
75+
.map(Transaction::getTrader)
76+
.filter(trader -> trader.getCity().equals("Milan"))
77+
.forEach(trader -> trader.setCity("Cambridge"));
78+
System.out.println(transactions);
79+
80+
81+
// Query 7: What's the highest value in all the transactions?
82+
int highestValue =
83+
transactions.stream()
84+
.map(Transaction::getValue)
85+
.reduce(0, Integer::max);
86+
System.out.println(highestValue);
87+
}
88+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package lambdasinaction.chap4;
2+
import java.util.stream.*;
3+
import java.util.*;
4+
5+
import static lambdasinaction.chap4.Dish.menu;
6+
7+
public class Reducing{
8+
9+
public static void main(String...args){
10+
11+
List<Integer> numbers = Arrays.asList(3,4,5,1,2);
12+
int sum = numbers.stream().reduce(0, (a, b) -> a + b);
13+
System.out.println(sum);
14+
15+
int sum2 = numbers.stream().reduce(0, Integer::sum);
16+
System.out.println(sum2);
17+
18+
int max = numbers.stream().reduce(0, (a, b) -> Integer.max(a, b));
19+
System.out.println(max);
20+
21+
}
22+
23+
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package lambdasinaction.chap4;
2+
3+
import java.util.*;
4+
import java.util.stream.*;
5+
import static java.util.stream.Collectors.toList;
6+
7+
import lambdasinaction.chap5.Dish;
8+
import static lambdasinaction.chap5.Dish.menu;
9+
10+
public class StreamBasic {
11+
12+
public static void main(String...args){
13+
List<String> names = menu.stream()
14+
.filter(d -> d.getCalories() > 300)
15+
.map(Dish::getName)
16+
.limit(3)
17+
.collect(toList());
18+
19+
System.out.println(names);
20+
21+
22+
}
23+
24+
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package lambdasinaction.chap4;
2+
3+
import java.util.*;
4+
import java.util.stream.*;
5+
import static java.util.stream.Collectors.toList;
6+
7+
8+
public class StreamVsCollection {
9+
10+
public static void main(String...args){
11+
List<String> names = Arrays.asList("Java8", "Lambdas", "In", "Action");
12+
Stream<String> s = names.stream();
13+
s.forEach(System.out::println);
14+
s.forEach(System.out::println); // Exception
15+
16+
}
17+
18+
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package lambdasinaction.chap4;
2+
public class Trader{
3+
4+
private String name;
5+
private String city;
6+
7+
public Trader(String n, String c){
8+
this.name = n;
9+
this.city = c;
10+
}
11+
12+
public String getName(){
13+
return this.name;
14+
}
15+
16+
public String getCity(){
17+
return this.city;
18+
}
19+
20+
public void setCity(String newCity){
21+
this.city = newCity;
22+
}
23+
24+
public String toString(){
25+
return "Trader:"+this.name + " in " + this.city;
26+
}
27+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package lambdasinaction.chap4;
2+
public class Transaction{
3+
4+
private Trader trader;
5+
private int year;
6+
private int value;
7+
8+
public Transaction(Trader trader, int year, int value)
9+
{
10+
this.trader = trader;
11+
this.year = year;
12+
this.value = value;
13+
}
14+
15+
public Trader getTrader(){
16+
return this.trader;
17+
}
18+
19+
public int getYear(){
20+
return this.year;
21+
}
22+
23+
public int getValue(){
24+
return this.value;
25+
}
26+
27+
public String toString(){
28+
return "{" + this.trader + ", " +
29+
"year: "+this.year+", " +
30+
"value:" + this.value +"}";
31+
}
32+
}

0 commit comments

Comments
 (0)