Skip to content

Commit 6ec8a0f

Browse files
committed
Added more code examples for chapter 4
1 parent df3c99c commit 6ec8a0f

File tree

4 files changed

+103
-14
lines changed

4 files changed

+103
-14
lines changed
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,47 @@
11
package lambdasinaction.chap4;
22
import java.util.stream.*;
33
import java.util.*;
4+
import static java.util.stream.Collectors.toList;
45

56
import static lambdasinaction.chap4.Dish.menu;
67

78
public class Filtering{
89

910
public static void main(String...args){
1011

12+
// Filtering with predicate
13+
List<Dish> vegetarianMenu =
14+
menu.stream()
15+
.filter(Dish::isVegetarian)
16+
.collect(toList());
17+
18+
vegetarianMenu.forEach(System.out::println);
19+
20+
// Filtering unique elements
1121
List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4);
1222
numbers.stream()
1323
.filter(i -> i % 2 == 0)
1424
.distinct()
1525
.forEach(System.out::println);
1626

17-
numbers.stream().limit(20).forEach(System.out::println);
27+
// Truncating a stream
28+
List<Dish> dishesLimit3 =
29+
menu.stream()
30+
.filter(d -> d.getCalories() > 300)
31+
.limit(3)
32+
.collect(toList());
33+
34+
dishesLimit3.forEach(System.out::println);
35+
36+
// Skipping elements
37+
List<Dish> dishesSkip2 =
38+
menu.stream()
39+
.filter(d -> d.getCalories() > 300)
40+
.skip(2)
41+
.collect(toList());
42+
43+
dishesSkip2.forEach(System.out::println);
44+
45+
1846
}
1947
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package lambdasinaction.chap4;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import static java.util.stream.Collectors.toList;
7+
8+
/**
9+
* Created by raoul-gabrielurma on 14/01/2014.
10+
*/
11+
public class Laziness {
12+
13+
public static void main(String[] args) {
14+
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
15+
List<Integer> twoEvenSquares =
16+
numbers.stream()
17+
.filter(n -> {
18+
System.out.println("filtering " + n); return n % 2 == 0;
19+
})
20+
.map(n -> {
21+
System.out.println("mapping " + n);
22+
return n * n;
23+
})
24+
.limit(2)
25+
.collect(toList());
26+
27+
}
28+
29+
30+
}

src/main/java/lambdasinaction/chap4/Mapping.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
package lambdasinaction.chap4;
2-
import java.util.stream.*;
2+
33
import java.util.*;
44
import static java.util.stream.Collectors.toList;
55
import static lambdasinaction.chap4.Dish.menu;
66

77
public class Mapping{
88

99
public static void main(String...args){
10-
10+
1111
// map
1212
List<String> dishNames = menu.stream()
1313
.map(Dish::getName)
1414
.collect(toList());
1515
System.out.println(dishNames);
16-
16+
1717
// map
1818
List<String> words = Arrays.asList("Hello", "World");
1919
List<Integer> wordLengths = words.stream()
2020
.map(String::length)
2121
.collect(toList());
2222
System.out.println(wordLengths);
23-
24-
// flatMap
23+
24+
// flatMap
2525
words.stream()
2626
.flatMap((String line) -> Arrays.stream(line.split("")))
2727
.distinct()
2828
.forEach(System.out::println);
29-
29+
3030
// flatMap
3131
List<Integer> numbers1 = Arrays.asList(1,2,3,4,5);
3232
List<Integer> numbers2 = Arrays.asList(6,7,8);
33-
List<int[]> pairs =
33+
List<int[]> pairs =
3434
numbers1.stream()
3535
.flatMap(i -> numbers2.stream()
3636
.map(j -> new int[]{i, j})

src/main/java/lambdasinaction/chap4/StreamBasic.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,49 @@
22

33
import java.util.*;
44
import java.util.stream.*;
5+
6+
import static java.util.Comparator.comparing;
57
import static java.util.stream.Collectors.toList;
68

79
import static lambdasinaction.chap4.Dish.menu;
810

911
public class StreamBasic {
1012

1113
public static void main(String...args){
12-
List<String> names = menu.stream()
13-
.filter(d -> d.getCalories() > 300)
14-
.map(Dish::getName)
15-
.limit(3)
16-
.collect(toList());
17-
System.out.println(names);
14+
// Java 7
15+
getLowCaloricDishesNamesInJava7(Dish.menu).forEach(System.out::println);
16+
17+
System.out.println("---");
18+
19+
// Java 8
20+
getLowCaloricDishesNamesInJava8(Dish.menu).forEach(System.out::println);
21+
22+
}
23+
24+
public static List<String> getLowCaloricDishesNamesInJava7(List<Dish> dishes){
25+
List<Dish> lowCaloricDishes = new ArrayList<>();
26+
for(Dish d: dishes){
27+
if(d.getCalories() > 400){
28+
lowCaloricDishes.add(d);
29+
}
30+
}
31+
List<String> lowCaloricDishesName = new ArrayList<>();
32+
Collections.sort(lowCaloricDishes, new Comparator<Dish>() {
33+
public int compare(Dish d1, Dish d2){
34+
return Integer.compare(d1.getCalories(), d2.getCalories());
35+
}
36+
});
37+
for(Dish d: lowCaloricDishes){
38+
lowCaloricDishesName.add(d.getName());
39+
}
40+
return lowCaloricDishesName;
41+
}
42+
43+
public static List<String> getLowCaloricDishesNamesInJava8(List<Dish> dishes){
44+
return dishes.stream()
45+
.filter(d -> d.getCalories() > 400)
46+
.sorted(comparing(Dish::getCalories))
47+
.map(Dish::getName)
48+
.collect(toList());
1849
}
1950
}

0 commit comments

Comments
 (0)