Skip to content

Commit 108fda3

Browse files
committed
chapter 4
1 parent 112fe26 commit 108fda3

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.hkbea.chap4;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class Dish {
7+
8+
private final String name;
9+
private final boolean vegetarian;
10+
private final int calories;
11+
private final Type type;
12+
13+
public Dish(String name, boolean vegetarian, int calories, Type type) {
14+
this.name = name;
15+
this.vegetarian = vegetarian;
16+
this.calories = calories;
17+
this.type = type;
18+
}
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public boolean isVegetarian() {
25+
return vegetarian;
26+
}
27+
28+
public int getCalories() {
29+
return calories;
30+
}
31+
32+
public Type getType() {
33+
return type;
34+
}
35+
36+
public enum Type {
37+
MEAT, FISH, OTHER
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return name;
43+
}
44+
45+
public static final List<Dish> menu = Arrays.asList(new Dish("pork", false, 800, Dish.Type.MEAT),
46+
new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT),
47+
new Dish("french fries", true, 530, Dish.Type.OTHER), new Dish("rice", true, 350, Dish.Type.OTHER),
48+
new Dish("season fruit", true, 120, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER),
49+
new Dish("prawns", false, 400, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH));
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.hkbea.chap4;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
public class StreamBasic {
10+
11+
public static void main(String[] args) {
12+
getLowCaloricDishesNamesInJava7(Dish.menu).forEach(str -> System.out.println(str));
13+
14+
System.out.println("----------------------------------------");
15+
getLowCaloricDishesNamesInJava8(Dish.menu).forEach(System.out::println);
16+
17+
List<String> threeHighCaloricDishNames = Dish.menu.parallelStream().filter(dish -> dish.getCalories() > 300).map(Dish::getName).limit(3).collect(Collectors.toList());
18+
System.out.println(threeHighCaloricDishNames);
19+
//4.4
20+
21+
List<String> names = Dish.menu.stream().filter(d -> {
22+
System.out.println("filtering " + d.getName());
23+
return d.getCalories() > 300;
24+
}).map(d -> {
25+
System.out.println("mapping " + d.getName());
26+
return d.getName();
27+
}).limit(3).collect(Collectors.toList());
28+
System.out.println(names);
29+
}
30+
31+
public static List<String> getLowCaloricDishesNamesInJava7(List<Dish> dishes) {
32+
List<Dish> lowCaloricDishes = new ArrayList<>();
33+
for(Dish dish:dishes) {
34+
if(dish.getCalories() < 400) {
35+
lowCaloricDishes.add(dish);
36+
}
37+
}
38+
39+
Collections.sort(lowCaloricDishes, Comparator.comparing(Dish::getCalories));
40+
41+
List<String> lowCaloricDishesName = new ArrayList<>();
42+
for(Dish dish : lowCaloricDishes) {
43+
lowCaloricDishesName.add(dish.getName());
44+
}
45+
return lowCaloricDishesName;
46+
}
47+
48+
public static List<String> getLowCaloricDishesNamesInJava8(List<Dish> dishes) {
49+
return dishes.parallelStream().filter((Dish dish) -> dish.getCalories() < 400).sorted(Comparator.comparing(Dish::getCalories)).map(dish -> dish.getName()).collect(Collectors.toList());
50+
}
51+
52+
}

0 commit comments

Comments
 (0)