Skip to content

Commit b58c4c9

Browse files
committed
mavenize project
1 parent 6cc5c3b commit b58c4c9

11 files changed

+175
-1
lines changed

.gitignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/target
2+
/local
3+
4+
# Eclipse, Netbeans and IntelliJ files
5+
/.*
6+
!.gitignore
7+
/nbproject
8+
/*.ipr
9+
/*.iws
10+
/*.iml
11+
12+
# Repository wide ignore mac DS_Store files
13+
.DS_Store

pom.xml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>manning</groupId>
8+
<artifactId>lambdasinaction</artifactId>
9+
<version>1.0</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<version>4.8.1</version>
16+
<scope>test</scope>
17+
</dependency>
18+
</dependencies>
19+
20+
<build>
21+
<plugins>
22+
<plugin>
23+
<groupId>org.apache.maven.plugins</groupId>
24+
<artifactId>maven-compiler-plugin</artifactId>
25+
<configuration>
26+
<source>8</source>
27+
<target>8</target>
28+
</configuration>
29+
</plugin>
30+
</plugins>
31+
</build>
32+
33+
</project>

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

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
package lambdasinaction.chap1;
2+
13
import java.util.*;
24
import java.util.function.Predicate;
5+
36
public class FilteringApples{
47

58
public static void main(String ... args){

chap2/FilteringApples.java src/main/java/lambdasinaction/chap2/FilteringApples.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
package lambdasinaction.chap2;
2+
13
import java.util.*;
4+
25
public class FilteringApples{
36

47
public static void main(String ... args){

chap2/MeaningOfThis.java src/main/java/lambdasinaction/chap2/MeaningOfThis.java

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package lambdasinaction.chap2;
2+
13
public class MeaningOfThis
24
{
35
public final int value = 4;

chap3/ExecuteAround.java src/main/java/lambdasinaction/chap3/ExecuteAround.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package lambdasinaction.chap3;
2+
13
import java.io.*;
24
public class ExecuteAround {
35

@@ -11,7 +13,7 @@ public static void main(String ...args) throws IOException{
1113
}
1214

1315
public static String processFile(BufferedReaderProcessor p) throws IOException {
14-
try(BufferedReader br = new BufferedReader(new FileReader("data.txt"))){
16+
try(BufferedReader br = new BufferedReader(new FileReader("src/main/java/lambdasinaction/chap3/data.txt"))){
1517
return p.process(br);
1618
}
1719

chap3/Lambdas.java src/main/java/lambdasinaction/chap3/Lambdas.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
package lambdasinaction.chap3;
2+
13
import java.util.*;
4+
25
public class Lambdas {
36
public static void main(String ...args){
47

chap3/Sorting.java src/main/java/lambdasinaction/chap3/Sorting.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
package lambdasinaction.chap3;
2+
13
import java.util.*;
24
import static java.util.Comparator.comparing;
5+
36
public class Sorting {
47

58
public static void main(String...args){
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package lambdasinaction.chap5;
2+
3+
public class Dish {
4+
private final String name;
5+
private final boolean vegetarian;
6+
private final int calories;
7+
private final Type type;
8+
9+
public Dish(String name, boolean vegetarian, int calories, Type type) {
10+
this.name = name;
11+
this.vegetarian = vegetarian;
12+
this.calories = calories;
13+
this.type = type;
14+
}
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public boolean isVegetarian() {
21+
return vegetarian;
22+
}
23+
24+
public int getCalories() {
25+
return calories;
26+
}
27+
28+
public Type getType() {
29+
return type;
30+
}
31+
32+
public enum Type { MEAT, FISH, OTHER }
33+
34+
@Override
35+
public String toString() {
36+
return name;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package lambdasinaction.chap5;
2+
3+
import java.util.*;
4+
5+
import static java.util.stream.Collectors.groupingBy;
6+
7+
public class GroupingTransactions {
8+
9+
public static List<Transaction> transactions = Arrays.asList( new Transaction(Currency.EUR, 1500.0),
10+
new Transaction(Currency.USD, 2300.0),
11+
new Transaction(Currency.GBP, 9900.0),
12+
new Transaction(Currency.EUR, 1100.0),
13+
new Transaction(Currency.JPY, 7800.0),
14+
new Transaction(Currency.CHF, 6700.0),
15+
new Transaction(Currency.EUR, 5600.0),
16+
new Transaction(Currency.USD, 4500.0),
17+
new Transaction(Currency.CHF, 3400.0),
18+
new Transaction(Currency.GBP, 3200.0),
19+
new Transaction(Currency.USD, 4600.0),
20+
new Transaction(Currency.JPY, 5700.0),
21+
new Transaction(Currency.EUR, 6800.0) );
22+
public static void main(String ... args) {
23+
groupImperatively();
24+
groupFunctionally();
25+
26+
}
27+
28+
private static void groupImperatively() {
29+
Map<Currency, List<Transaction>> transactionsByCurrencies = new HashMap<>();
30+
for (Transaction transaction : transactions) {
31+
Currency currency = transaction.getCurrency();
32+
List<Transaction> transactionsForCurrency = transactionsByCurrencies.get(currency);
33+
if (transactionsForCurrency == null) {
34+
transactionsForCurrency = new ArrayList<>();
35+
transactionsByCurrencies.put(currency, transactionsForCurrency);
36+
}
37+
transactionsForCurrency.add(transaction);
38+
}
39+
40+
System.out.println(transactionsByCurrencies);
41+
}
42+
43+
private static void groupFunctionally() {
44+
Map<Currency, List<Transaction>> transactionsByCurrencies = transactions.stream().collect(groupingBy(Transaction::getCurrency));
45+
System.out.println(transactionsByCurrencies);
46+
}
47+
48+
public static class Transaction {
49+
private final Currency currency;
50+
private final double value;
51+
52+
public Transaction(Currency currency, double value) {
53+
this.currency = currency;
54+
this.value = value;
55+
}
56+
57+
public Currency getCurrency() {
58+
return currency;
59+
}
60+
61+
public double getValue() {
62+
return value;
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return currency + " " + value;
68+
}
69+
}
70+
71+
public enum Currency {
72+
EUR, USD, JPY, GBP, CHF
73+
}
74+
}

0 commit comments

Comments
 (0)