├── .gitignore
├── LICENSE
├── README.md
├── pom.xml
└── src
└── main
├── java
└── lambdasinaction
│ ├── appa
│ ├── Author.java
│ ├── Authors.java
│ └── Book.java
│ ├── appc
│ ├── StreamForker.java
│ └── StreamForkerExample.java
│ ├── appd
│ ├── InnerClass.java
│ └── Lambda.java
│ ├── chap1
│ └── FilteringApples.java
│ ├── chap10
│ ├── Car.java
│ ├── Insurance.java
│ ├── OperationsWithOptional.java
│ ├── OptionalMain.java
│ ├── Person.java
│ └── ReadPositiveIntParam.java
│ ├── chap11
│ ├── AsyncShop.java
│ ├── AsyncShopClient.java
│ ├── BestPriceFinder.java
│ ├── BestPriceFinderMain.java
│ ├── Discount.java
│ ├── ExchangeService.java
│ ├── Quote.java
│ ├── Shop.java
│ ├── Util.java
│ └── v1
│ │ ├── BestPriceFinder.java
│ │ ├── BestPriceFinderMain.java
│ │ ├── Shop.java
│ │ └── ShopMain.java
│ ├── chap12
│ └── DateTimeExamples.java
│ ├── chap13
│ ├── Recursion.java
│ └── SubsetsMain.java
│ ├── chap14
│ ├── Combinators.java
│ ├── Currying.java
│ ├── LazyLists.java
│ ├── PatternMatching.java
│ ├── PersistentTrainJourney.java
│ └── PersistentTree.java
│ ├── chap2
│ ├── FilteringApples.java
│ └── MeaningOfThis.java
│ ├── chap3
│ ├── ExecuteAround.java
│ ├── Lambdas.java
│ └── Sorting.java
│ ├── chap4
│ ├── Dish.java
│ ├── StreamBasic.java
│ └── StreamVsCollection.java
│ ├── chap5
│ ├── BuildingStreams.java
│ ├── Filtering.java
│ ├── Finding.java
│ ├── Laziness.java
│ ├── Mapping.java
│ ├── NumericStreams.java
│ ├── PuttingIntoPractice.java
│ ├── Reducing.java
│ ├── Trader.java
│ └── Transaction.java
│ ├── chap6
│ ├── CollectorHarness.java
│ ├── Dish.java
│ ├── Grouping.java
│ ├── GroupingTransactions.java
│ ├── PartitionPrimeNumbers.java
│ ├── Partitioning.java
│ ├── Reducing.java
│ ├── Summarizing.java
│ └── ToListCollector.java
│ ├── chap7
│ ├── ForkJoinSumCalculator.java
│ ├── ParallelStreamBenchmark.java
│ ├── ParallelStreams.java
│ ├── ParallelStreamsHarness.java
│ └── WordCount.java
│ ├── chap8
│ ├── ChainOfResponsibilityMain.java
│ ├── Debugging.java
│ ├── FactoryMain.java
│ ├── ObserverMain.java
│ ├── OnlineBanking.java
│ ├── OnlineBankingLambda.java
│ ├── Peek.java
│ └── StrategyMain.java
│ ├── chap9
│ ├── Ambiguous.java
│ ├── Diamond.java
│ ├── Drawable.java
│ ├── Ellipse.java
│ ├── Game.java
│ ├── Intro.java
│ ├── Letter.java
│ ├── MostSpecific.java
│ ├── README
│ ├── Resizable.java
│ ├── Square.java
│ ├── Triangle.java
│ └── Utils.java
│ └── dsl
│ ├── Grouping.java
│ ├── LambdaOrderBuilder.java
│ ├── Main.java
│ ├── MethodChainingOrderBuilder.java
│ ├── Mixed.java
│ ├── MixedBuilder.java
│ ├── NestedFunctionOrderBuilder.java
│ ├── TaxCalculator.java
│ └── model
│ ├── Order.java
│ ├── Stock.java
│ ├── Tax.java
│ └── Trade.java
└── resources
└── lambdasinaction
├── chap3
└── data.txt
└── chap5
└── data.txt
/.gitignore:
--------------------------------------------------------------------------------
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
14 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2013 Raoul-Gabriel Urma, Mario Fusco, Alan Mycroft
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Java8InAction
2 | ===============
3 |
4 | This repository contains all the source code for the examples and quizzes in the book Java 8 in Action: Lambdas, Streams and functional-style programming.
5 |
6 | You can purchase the book here: [http://manning.com/urma/](http://manning.com/urma/) or on Amazon
7 |
8 | The source code for all examples can be found in the directory [src/main/java/lambdasinaction](https://github.com/java8/Java8InAction/tree/master/src/main/java/lambdasinaction)
9 |
10 | * Chapter 1: Java 8: why should you care?
11 | * Chapter 2: Passing code with behavior parameterization
12 | * Chapter 3: Lambda expressions
13 | * Chapter 4: Working with Streams
14 | * Chapter 5: Processing data with streams
15 | * Chapter 6: Collecting data with streams
16 | * Chapter 7: Parallel data processing and performance
17 | * Chapter 8: Refactoring, testing, debugging
18 | * Chapter 9: Default methods
19 | * Chapter 10: Using Optional as a better alternative to null
20 | * Chapter 11: CompletableFuture: composable asynchronous programming
21 | * Chapter 12: New Date and Time API
22 | * Chapter 13: Thinking functionally
23 | * Chapter 14: Functional programming techniques
24 | * Chapter 15: Blending OOP and FP: comparing Java 8 and Scala
25 | * Chapter 16: Conclusions and "where next" for Java
26 | * Appendix A: Miscellaneous language updates
27 | * Appendix B: Miscellaneous library updates
28 | * Appendix C: Performing multiple operations in parallel on a Stream
29 | * Appendix D: Lambdas and JVM bytecode
30 | We will update the repository as we update the book. Stay tuned!
31 |
32 | ### Make sure to have JDK8 installed
33 | The latest binary can be found here: http://www.oracle.com/technetwork/java/javase/overview/java8-2100321.html
34 |
35 | $ java -version
36 |
37 | java version "1.8.0_05"
38 | Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
39 | Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)
40 |
41 |
42 | You can download a preview version here: https://jdk8.java.net/
43 |
44 | ### Compile/Run the examples
45 | Using maven:
46 |
47 | $ mvn compile
48 |
49 | $ cd target/classes
50 |
51 | $ java lambdasinaction/chap1/FilteringApples
52 |
53 |
54 | Alternatively you can compile the files manually inside the directory src/main/java
55 |
56 | You can also import the project in your favorite IDE:
57 | * In IntelliJ use "File->Open" menu and navigate to the folder where the project resides
58 | * In Eclipse use "File->Import->Existing Maven Projects" (also modify "Reduntant super interfaces" to report as Warnings instead of Errors
59 | * In Netbeans use "File->Open Project" menu
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | manning
8 | lambdasinaction
9 | 1.0
10 |
11 |
12 | UTF-8
13 |
14 |
15 |
16 |
17 | org.openjdk.jmh
18 | jmh-core
19 | 1.17.4
20 |
21 |
22 | org.openjdk.jmh
23 | jmh-generator-annprocess
24 | 1.17.4
25 |
26 |
27 | junit
28 | junit
29 | 4.11
30 |
31 |
32 |
33 |
34 |
35 |
36 | org.apache.maven.plugins
37 | maven-compiler-plugin
38 | 3.1
39 |
40 | 1.9
41 | 1.9
42 |
43 |
44 |
45 | org.apache.maven.plugins
46 | maven-shade-plugin
47 |
48 |
49 | package
50 |
51 | shade
52 |
53 |
54 | benchmarks
55 |
56 |
57 | org.openjdk.jmh.Main
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/src/main/java/lambdasinaction/appa/Author.java:
--------------------------------------------------------------------------------
1 | package lambdasinaction.appa;
2 |
3 | import java.lang.annotation.Repeatable;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 |
7 | @Repeatable(Authors.class)
8 | @Retention(RetentionPolicy.RUNTIME)
9 | public @interface Author {
10 |
11 | String name();
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/lambdasinaction/appa/Authors.java:
--------------------------------------------------------------------------------
1 | package lambdasinaction.appa;
2 |
3 | import java.lang.annotation.Retention;
4 | import java.lang.annotation.RetentionPolicy;
5 |
6 | @Retention(RetentionPolicy.RUNTIME)
7 | public @interface Authors {
8 |
9 | Author[] value();
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/src/main/java/lambdasinaction/appa/Book.java:
--------------------------------------------------------------------------------
1 | package lambdasinaction.appa;
2 |
3 | import java.util.Arrays;
4 |
5 | @Author(name = "Raoul")
6 | @Author(name = "Mario")
7 | @Author(name = "Alan")
8 | public class Book {
9 |
10 | public static void main(String[] args) {
11 | Author[] authors = Book.class.getAnnotationsByType(Author.class);
12 | Arrays.asList(authors).stream().forEach(a -> {
13 | System.out.println(a.name());
14 | });
15 | }
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/lambdasinaction/appc/StreamForker.java:
--------------------------------------------------------------------------------
1 | package lambdasinaction.appc;
2 |
3 | import java.util.*;
4 | import java.util.concurrent.*;
5 | import java.util.function.*;
6 | import java.util.stream.*;
7 |
8 | /**
9 | * Adapted from http://mail.openjdk.java.net/pipermail/lambda-dev/2013-November/011516.html
10 | */
11 | public class StreamForker {
12 |
13 | private final Stream stream;
14 | private final Map