Skip to content

Commit 355cd31

Browse files
Merge pull request #82 from sayed-moin-ahmed/develop
Stream
2 parents 6093cf3 + b872869 commit 355cd31

File tree

7 files changed

+106
-7
lines changed

7 files changed

+106
-7
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.List;
66

77
public class Data {
8-
public static List<Apple> getData() {
8+
public static List<Apple> getApples() {
99
Apple apple1 = new Apple(10, Colors.GREEN);
1010
Apple apple2 = new Apple(1,Colors.RED);
1111
Apple apple3 = new Apple(5,Colors.GREEN);
@@ -16,7 +16,7 @@ public static List<Apple> getData() {
1616
return apples;
1717
}
1818

19-
public static List<Apple> getData1() {
19+
public static List<Apple> getApples1() {
2020
Apple apple1 = new Apple(10,Colors.GREEN);
2121
Apple apple2 = new Apple(1,Colors.RED);
2222
Apple apple3 = new Apple(5,Colors.GREEN);
@@ -33,4 +33,15 @@ public static List<Apple> getData1() {
3333
apples.sort(Comparator.comparing(Apple::getWeight));
3434
return apples;
3535
}
36+
37+
public static List<Dish> getDishes(){
38+
return List.of(
39+
new Dish(Type.INDIAN,"Pav Bhaji",50),
40+
new Dish(Type.WESTERN,"Chicken Roast",250),
41+
new Dish(Type.CHINESE,"Noodles",150),
42+
new Dish(Type.INDIAN,"Bhaji",150),
43+
new Dish(Type.WESTERN,"Lamb ",550),
44+
new Dish(Type.CHINESE,"Prawn Noodles",250)
45+
);
46+
}
3647
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.java15.example.modern_java_in_action;
2+
3+
public class Dish {
4+
5+
private Type type;
6+
private String name;
7+
private int calories;
8+
9+
public Dish(){}
10+
11+
public Dish(Type type, String name, int calories) {
12+
this.type = type;
13+
this.name = name;
14+
this.calories = calories;
15+
}
16+
17+
public Type getType() {
18+
return type;
19+
}
20+
21+
public void setType(Type type) {
22+
this.type = type;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
public int getCalories() {
34+
return calories;
35+
}
36+
37+
public void setCalories(int calories) {
38+
this.calories = calories;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return "Dish{" +
44+
"type=" + type +
45+
", name='" + name + '\'' +
46+
", calories=" + calories +
47+
'}';
48+
}
49+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.java15.example.modern_java_in_action;
2+
3+
public enum Type {
4+
INDIAN,
5+
WESTERN,
6+
CHINESE;
7+
}

src/main/java/com/java15/example/modern_java_in_action/ch1_java8_9_10_11_whatshappening/Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public class Test {
1414

1515
public static void main(String[] args) {
16-
List<Apple> apples = Data.getData();
16+
List<Apple> apples = Data.getApples();
1717
//filterApples(apples,(Apple a) -> Colors.GREEN.equals(a.getColor())).stream().forEach(System.out::println);
1818
// filterApples(apples,(Apple a) -> Colors.GREEN.equals(a.getColor())).stream().forEach(System.out::println);
1919
// filterGroupApples(apples,(Apple a) -> Colors.RED.equals(a.getColor())).forEach((i,list) ->{System.out.println(i+""+list);});

src/main/java/com/java15/example/modern_java_in_action/ch2_passingcodewithbehaviorparameterization/Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
public class Test {
1818
public static void main(String[] args) throws ExecutionException, InterruptedException {
19-
List<Apple> apples = Data.getData();
19+
List<Apple> apples = Data.getApples();
2020
// beforeJava8(apples);
2121
//filter(apples,(Apple e) -> Colors.GREEN.equals(e.getColor())).forEach(System.out::println);
2222
// groupBy(apples,apple -> apple.getWeight()).forEach((i, list)->System.out.println(i+""+list));

src/main/java/com/java15/example/modern_java_in_action/ch3_lambdaexpressions/Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ private static void extracted8() {
4646
}
4747

4848
private static void extracted7() {
49-
Data.getData().stream().filter(notRedApple).forEach(System.out::println);
50-
Data.getData().stream().filter(redAppleWithConditions).forEach(System.out::println);
49+
Data.getApples().stream().filter(notRedApple).forEach(System.out::println);
50+
Data.getApples().stream().filter(redAppleWithConditions).forEach(System.out::println);
5151
}
5252

5353
static Predicate<Apple> redApple = apple -> Colors.RED.equals(apple.getColor());
@@ -56,7 +56,7 @@ private static void extracted7() {
5656

5757

5858
private static void extracted6() {
59-
List<Apple> list = Data.getData1();
59+
List<Apple> list = Data.getApples1();
6060
list.sort(comparing(Apple::getWeight).reversed());
6161
list.forEach(System.out::println);
6262
System.out.println("++++++++++++++++++++++++++++++");
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.java15.example.modern_java_in_action.ch4_introducingstreams;
2+
3+
import com.java15.example.modern_java_in_action.Data;
4+
import com.java15.example.modern_java_in_action.Dish;
5+
import com.java15.example.modern_java_in_action.Type;
6+
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import static java.util.stream.Collectors.groupingBy;
11+
12+
public class Test {
13+
public static void main(String[] args) {
14+
List<Dish> dishes = Data.getDishes();
15+
/* List<String> dishNames1 = dishes.stream()
16+
.filter(dish -> dish.getCalories()>250)
17+
.sorted(Comparator.comparing(Dish::getName))
18+
.map(Dish::getName)
19+
.collect(Collectors.toList());
20+
dishNames1.forEach(System.out::println);
21+
22+
List<String> dishNames2 = dishes.parallelStream()
23+
.filter(dish -> dish.getCalories()>50)
24+
.sorted(Comparator.comparing(Dish::getName))
25+
.map(Dish::getName)
26+
.collect(Collectors.toList());
27+
dishNames2.forEach(System.out::println);*/
28+
29+
Map<Type,List<Dish>> map = dishes.stream().collect(groupingBy(Dish::getType));
30+
map.forEach((s,list)->System.out.println(s+""+list));
31+
}
32+
}

0 commit comments

Comments
 (0)