Skip to content

Commit eda3cce

Browse files
author
spean90
committed
..
1 parent 4786c46 commit eda3cce

7 files changed

Lines changed: 296 additions & 296 deletions

File tree

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
<plugin>
3636
<groupId>org.apache.maven.plugins</groupId>
3737
<artifactId>maven-compiler-plugin</artifactId>
38-
<version>3.1</version>
38+
<version>3.3</version>
3939
<configuration>
40-
<source>1.9</source>
41-
<target>1.9</target>
40+
<source>1.8</source>
41+
<target>1.8</target>
4242
</configuration>
4343
</plugin>
4444
<plugin>

src/main/java/lambdasinaction/chap1/FilteringApples.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,25 @@ public static void main(String ... args){
3333
System.out.println(weirdApples);
3434
}
3535

36-
public static List<Apple> filterGreenApples(List<Apple> inventory){
37-
List<Apple> result = new ArrayList<>();
38-
for (Apple apple: inventory){
39-
if ("green".equals(apple.getColor())) {
40-
result.add(apple);
41-
}
42-
}
43-
return result;
44-
}
45-
46-
public static List<Apple> filterHeavyApples(List<Apple> inventory){
47-
List<Apple> result = new ArrayList<>();
48-
for (Apple apple: inventory){
49-
if (apple.getWeight() > 150) {
50-
result.add(apple);
51-
}
52-
}
53-
return result;
54-
}
36+
// public static List<Apple> filterGreenApples(List<Apple> inventory){
37+
// List<Apple> result = new ArrayList<>();
38+
// for (Apple apple: inventory){
39+
// if ("green".equals(apple.getColor())) {
40+
// result.add(apple);
41+
// }
42+
// }
43+
// return result;
44+
// }
45+
46+
// public static List<Apple> filterHeavyApples(List<Apple> inventory){
47+
// List<Apple> result = new ArrayList<>();
48+
// for (Apple apple: inventory){
49+
// if (apple.getWeight() > 150) {
50+
// result.add(apple);
51+
// }
52+
// }
53+
// return result;
54+
// }
5555

5656
public static boolean isGreenApple(Apple apple) {
5757
return "green".equals(apple.getColor());
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
package lambdasinaction.chap10;
2-
3-
import java.util.*;
4-
5-
import static java.util.Optional.of;
6-
import static java.util.Optional.empty;
7-
8-
public class OperationsWithOptional {
9-
10-
public static void main(String... args) {
11-
System.out.println(max(of(3), of(5)));
12-
System.out.println(max(empty(), of(5)));
13-
14-
Optional<Integer> opt1 = of(5);
15-
Optional<Integer> opt2 = opt1.or(() -> of(4));
16-
17-
System.out.println(
18-
of(5).or(() -> of(4))
19-
);
20-
}
21-
22-
public static final Optional<Integer> max(Optional<Integer> i, Optional<Integer> j) {
23-
return i.flatMap(a -> j.map(b -> Math.max(a, b)));
24-
}
25-
}
1+
//package lambdasinaction.chap10;
2+
//
3+
//import java.util.*;
4+
//
5+
//import static java.util.Optional.of;
6+
//import static java.util.Optional.empty;
7+
//
8+
//public class OperationsWithOptional {
9+
//
10+
// public static void main(String... args) {
11+
// System.out.println(max(of(3), of(5)));
12+
// System.out.println(max(empty(), of(5)));
13+
//
14+
// Optional<Integer> opt1 = of(5);
15+
// Optional<Integer> opt2 = opt1.or(() -> of(4));
16+
//
17+
// System.out.println(
18+
// of(5).or(() -> of(4))
19+
// );
20+
// }
21+
//
22+
// public static final Optional<Integer> max(Optional<Integer> i, Optional<Integer> j) {
23+
// return i.flatMap(a -> j.map(b -> Math.max(a, b)));
24+
// }
25+
//}
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
package lambdasinaction.chap10;
2-
3-
import java.util.*;
4-
5-
import static java.util.stream.Collectors.toSet;
6-
7-
public class OptionalMain {
8-
9-
public String getCarInsuranceName(Optional<Person> person) {
10-
return person.flatMap(Person::getCar)
11-
.flatMap(Car::getInsurance)
12-
.map(Insurance::getName)
13-
.orElse("Unknown");
14-
}
15-
16-
public Set<String> getCarInsuranceNames(List<Person> persons) {
17-
return persons.stream()
18-
.map(Person::getCar)
19-
.map(optCar -> optCar.flatMap(Car::getInsurance))
20-
.map(optInsurance -> optInsurance.map(Insurance::getName))
21-
.flatMap(Optional::stream)
22-
.collect(toSet());
23-
}
24-
}
1+
//package lambdasinaction.chap10;
2+
//
3+
//import java.util.*;
4+
//
5+
//import static java.util.stream.Collectors.toSet;
6+
//
7+
//public class OptionalMain {
8+
//
9+
// public String getCarInsuranceName(Optional<Person> person) {
10+
// return person.flatMap(Person::getCar)
11+
// .flatMap(Car::getInsurance)
12+
// .map(Insurance::getName)
13+
// .orElse("Unknown");
14+
// }
15+
//
16+
// public Set<String> getCarInsuranceNames(List<Person> persons) {
17+
// return persons.stream()
18+
// .map(Person::getCar)
19+
// .map(optCar -> optCar.flatMap(Car::getInsurance))
20+
// .map(optInsurance -> optInsurance.map(Insurance::getName))
21+
// .flatMap(Optional::stream)
22+
// .collect(toSet());
23+
// }
24+
//}
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
package lambdasinaction.chap6;
2-
3-
import java.util.function.*;
4-
5-
public class CollectorHarness {
6-
7-
public static void main(String[] args) {
8-
//System.out.println("Partitioning done in: " + execute(PartitionPrimeNumbers::partitionPrimes) + " msecs");
9-
System.out.println("Partitioning done in: " + execute(PartitionPrimeNumbers::partitionPrimesWithCustomCollector) + " msecs" );
10-
}
11-
12-
private static long execute(Consumer<Integer> primePartitioner) {
13-
long fastest = Long.MAX_VALUE;
14-
for (int i = 0; i < 10; i++) {
15-
long start = System.nanoTime();
16-
primePartitioner.accept(1_000_000);
17-
long duration = (System.nanoTime() - start) / 1_000_000;
18-
if (duration < fastest) fastest = duration;
19-
System.out.println("done in " + duration);
20-
}
21-
return fastest;
22-
}
23-
}
1+
//package lambdasinaction.chap6;
2+
//
3+
//import java.util.function.*;
4+
//
5+
//public class CollectorHarness {
6+
//
7+
// public static void main(String[] args) {
8+
// //System.out.println("Partitioning done in: " + execute(PartitionPrimeNumbers::partitionPrimes) + " msecs");
9+
// System.out.println("Partitioning done in: " + execute(PartitionPrimeNumbers::partitionPrimesWithCustomCollector) + " msecs" );
10+
// }
11+
//
12+
// private static long execute(Consumer<Integer> primePartitioner) {
13+
// long fastest = Long.MAX_VALUE;
14+
// for (int i = 0; i < 10; i++) {
15+
// long start = System.nanoTime();
16+
// primePartitioner.accept(1_000_000);
17+
// long duration = (System.nanoTime() - start) / 1_000_000;
18+
// if (duration < fastest) fastest = duration;
19+
// System.out.println("done in " + duration);
20+
// }
21+
// return fastest;
22+
// }
23+
//}
Lines changed: 96 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,96 @@
1-
package lambdasinaction.chap6;
2-
3-
import java.util.*;
4-
5-
import static java.util.stream.Collectors.*;
6-
import static lambdasinaction.chap6.Dish.dishTags;
7-
import static lambdasinaction.chap6.Dish.menu;
8-
9-
public class Grouping {
10-
11-
enum CaloricLevel { DIET, NORMAL, FAT };
12-
13-
public static void main(String ... args) {
14-
System.out.println("Dishes grouped by type: " + groupDishesByType());
15-
System.out.println("Dish names grouped by type: " + groupDishNamesByType());
16-
System.out.println("Dish tags grouped by type: " + groupDishTagsByType());
17-
System.out.println("Caloric dishes grouped by type: " + groupCaloricDishesByType());
18-
System.out.println("Dishes grouped by caloric level: " + groupDishesByCaloricLevel());
19-
System.out.println("Dishes grouped by type and caloric level: " + groupDishedByTypeAndCaloricLevel());
20-
System.out.println("Count dishes in groups: " + countDishesInGroups());
21-
System.out.println("Most caloric dishes by type: " + mostCaloricDishesByType());
22-
System.out.println("Most caloric dishes by type: " + mostCaloricDishesByTypeWithoutOprionals());
23-
System.out.println("Sum calories by type: " + sumCaloriesByType());
24-
System.out.println("Caloric levels by type: " + caloricLevelsByType());
25-
}
26-
27-
private static Map<Dish.Type, List<Dish>> groupDishesByType() {
28-
return menu.stream().collect(groupingBy(Dish::getType));
29-
}
30-
31-
private static Map<Dish.Type, List<String>> groupDishNamesByType() {
32-
return menu.stream().collect(groupingBy(Dish::getType, mapping(Dish::getName, toList())));
33-
}
34-
35-
private static Map<Dish.Type, Set<String>> groupDishTagsByType() {
36-
return menu.stream().collect(groupingBy(Dish::getType, flatMapping(dish -> dishTags.get( dish.getName() ).stream(), toSet())));
37-
}
38-
39-
private static Map<Dish.Type, List<Dish>> groupCaloricDishesByType() {
40-
// return menu.stream().filter(dish -> dish.getCalories() > 500).collect(groupingBy(Dish::getType));
41-
return menu.stream().collect(groupingBy(Dish::getType, filtering(dish -> dish.getCalories() > 500, toList())));
42-
}
43-
44-
private static Map<CaloricLevel, List<Dish>> groupDishesByCaloricLevel() {
45-
return menu.stream().collect(
46-
groupingBy(dish -> {
47-
if (dish.getCalories() <= 400) return CaloricLevel.DIET;
48-
else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL;
49-
else return CaloricLevel.FAT;
50-
} ));
51-
}
52-
53-
private static Map<Dish.Type, Map<CaloricLevel, List<Dish>>> groupDishedByTypeAndCaloricLevel() {
54-
return menu.stream().collect(
55-
groupingBy(Dish::getType,
56-
groupingBy((Dish dish) -> {
57-
if (dish.getCalories() <= 400) return CaloricLevel.DIET;
58-
else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL;
59-
else return CaloricLevel.FAT;
60-
} )
61-
)
62-
);
63-
}
64-
65-
private static Map<Dish.Type, Long> countDishesInGroups() {
66-
return menu.stream().collect(groupingBy(Dish::getType, counting()));
67-
}
68-
69-
private static Map<Dish.Type, Optional<Dish>> mostCaloricDishesByType() {
70-
return menu.stream().collect(
71-
groupingBy(Dish::getType,
72-
reducing((Dish d1, Dish d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2)));
73-
}
74-
75-
private static Map<Dish.Type, Dish> mostCaloricDishesByTypeWithoutOprionals() {
76-
return menu.stream().collect(
77-
groupingBy(Dish::getType,
78-
collectingAndThen(
79-
reducing((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2),
80-
Optional::get)));
81-
}
82-
83-
private static Map<Dish.Type, Integer> sumCaloriesByType() {
84-
return menu.stream().collect(groupingBy(Dish::getType,
85-
summingInt(Dish::getCalories)));
86-
}
87-
88-
private static Map<Dish.Type, Set<CaloricLevel>> caloricLevelsByType() {
89-
return menu.stream().collect(
90-
groupingBy(Dish::getType, mapping(
91-
dish -> { if (dish.getCalories() <= 400) return CaloricLevel.DIET;
92-
else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL;
93-
else return CaloricLevel.FAT; },
94-
toSet() )));
95-
}
96-
}
1+
//package lambdasinaction.chap6;
2+
//
3+
//import java.util.*;
4+
//
5+
//import static java.util.stream.Collectors.*;
6+
//import static lambdasinaction.chap6.Dish.dishTags;
7+
//import static lambdasinaction.chap6.Dish.menu;
8+
//
9+
//public class Grouping {
10+
//
11+
// enum CaloricLevel { DIET, NORMAL, FAT };
12+
//
13+
// public static void main(String ... args) {
14+
// System.out.println("Dishes grouped by type: " + groupDishesByType());
15+
// System.out.println("Dish names grouped by type: " + groupDishNamesByType());
16+
// System.out.println("Dish tags grouped by type: " + groupDishTagsByType());
17+
// System.out.println("Caloric dishes grouped by type: " + groupCaloricDishesByType());
18+
// System.out.println("Dishes grouped by caloric level: " + groupDishesByCaloricLevel());
19+
// System.out.println("Dishes grouped by type and caloric level: " + groupDishedByTypeAndCaloricLevel());
20+
// System.out.println("Count dishes in groups: " + countDishesInGroups());
21+
// System.out.println("Most caloric dishes by type: " + mostCaloricDishesByType());
22+
// System.out.println("Most caloric dishes by type: " + mostCaloricDishesByTypeWithoutOprionals());
23+
// System.out.println("Sum calories by type: " + sumCaloriesByType());
24+
// System.out.println("Caloric levels by type: " + caloricLevelsByType());
25+
// }
26+
//
27+
// private static Map<Dish.Type, List<Dish>> groupDishesByType() {
28+
// return menu.stream().collect(groupingBy(Dish::getType));
29+
// }
30+
//
31+
// private static Map<Dish.Type, List<String>> groupDishNamesByType() {
32+
// return menu.stream().collect(groupingBy(Dish::getType, mapping(Dish::getName, toList())));
33+
// }
34+
//
35+
// private static Map<Dish.Type, Set<String>> groupDishTagsByType() {
36+
// return menu.stream().collect(groupingBy(Dish::getType, flatMapping(dish -> dishTags.get( dish.getName() ).stream(), toSet())));
37+
// }
38+
//
39+
// private static Map<Dish.Type, List<Dish>> groupCaloricDishesByType() {
40+
//// return menu.stream().filter(dish -> dish.getCalories() > 500).collect(groupingBy(Dish::getType));
41+
// return menu.stream().collect(groupingBy(Dish::getType, filtering(dish -> dish.getCalories() > 500, toList())));
42+
// }
43+
//
44+
// private static Map<CaloricLevel, List<Dish>> groupDishesByCaloricLevel() {
45+
// return menu.stream().collect(
46+
// groupingBy(dish -> {
47+
// if (dish.getCalories() <= 400) return CaloricLevel.DIET;
48+
// else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL;
49+
// else return CaloricLevel.FAT;
50+
// } ));
51+
// }
52+
//
53+
// private static Map<Dish.Type, Map<CaloricLevel, List<Dish>>> groupDishedByTypeAndCaloricLevel() {
54+
// return menu.stream().collect(
55+
// groupingBy(Dish::getType,
56+
// groupingBy((Dish dish) -> {
57+
// if (dish.getCalories() <= 400) return CaloricLevel.DIET;
58+
// else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL;
59+
// else return CaloricLevel.FAT;
60+
// } )
61+
// )
62+
// );
63+
// }
64+
//
65+
// private static Map<Dish.Type, Long> countDishesInGroups() {
66+
// return menu.stream().collect(groupingBy(Dish::getType, counting()));
67+
// }
68+
//
69+
// private static Map<Dish.Type, Optional<Dish>> mostCaloricDishesByType() {
70+
// return menu.stream().collect(
71+
// groupingBy(Dish::getType,
72+
// reducing((Dish d1, Dish d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2)));
73+
// }
74+
//
75+
// private static Map<Dish.Type, Dish> mostCaloricDishesByTypeWithoutOprionals() {
76+
// return menu.stream().collect(
77+
// groupingBy(Dish::getType,
78+
// collectingAndThen(
79+
// reducing((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2),
80+
// Optional::get)));
81+
// }
82+
//
83+
// private static Map<Dish.Type, Integer> sumCaloriesByType() {
84+
// return menu.stream().collect(groupingBy(Dish::getType,
85+
// summingInt(Dish::getCalories)));
86+
// }
87+
//
88+
// private static Map<Dish.Type, Set<CaloricLevel>> caloricLevelsByType() {
89+
// return menu.stream().collect(
90+
// groupingBy(Dish::getType, mapping(
91+
// dish -> { if (dish.getCalories() <= 400) return CaloricLevel.DIET;
92+
// else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL;
93+
// else return CaloricLevel.FAT; },
94+
// toSet() )));
95+
// }
96+
//}

0 commit comments

Comments
 (0)