Skip to content

Commit 9abe29f

Browse files
Filters
1 parent 206c706 commit 9abe29f

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/main/java/com/java15/example/modern_java_in_action/Dish.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ public Dish(Type type, String name, int calories) {
1414
this.calories = calories;
1515
}
1616

17+
public Dish(String name, boolean b, int calories, Type type) {
18+
this.name = name;
19+
this.calories = calories;
20+
this.type = type;
21+
}
22+
1723
public Type getType() {
1824
return type;
1925
}

src/main/java/com/java15/example/modern_java_in_action/Type.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.java15.example.modern_java_in_action;
22

33
public enum Type {
4+
OTHER,
5+
FISH,
6+
MEAT,
47
INDIAN,
58
WESTERN,
69
CHINESE;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.java15.example.modern_java_in_action.ch5_workingwithstream;
2+
3+
import com.java15.example.modern_java_in_action.Dish;
4+
import com.java15.example.modern_java_in_action.Type;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.stream.Collectors;
9+
10+
import static java.util.stream.Collectors.toList;
11+
12+
public class Test {
13+
public static void main(String[] args){
14+
List<Dish> specialMenu = Arrays.asList(
15+
new Dish("seasonal fruit", true, 120, Type.OTHER),
16+
new Dish("prawns", false, 300, Type.FISH),
17+
new Dish("rice", true, 350, Type.OTHER),
18+
new Dish("chicken", false, 400, Type.MEAT),
19+
new Dish("french fries", true, 530, Type.OTHER));
20+
21+
22+
specialMenu.stream().filter(dish->dish.getCalories()<320).collect(toList()).forEach(System.out::println);
23+
}
24+
}

0 commit comments

Comments
 (0)