Code Monkey home page Code Monkey logo

java8inaction's Introduction

Java8InAction

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.

You can purchase the book here: http://manning.com/urma/ or on Amazon

The source code for all examples can be found in the directory src/main/java/lambdasinaction

  • Chapter 1: Java 8: why should you care?
  • Chapter 2: Passing code with behavior parameterization
  • Chapter 3: Lambda expressions
  • Chapter 4: Working with Streams
  • Chapter 5: Processing data with streams
  • Chapter 6: Collecting data with streams
  • Chapter 7: Parallel data processing and performance
  • Chapter 8: Refactoring, testing, debugging
  • Chapter 9: Default methods
  • Chapter 10: Using Optional as a better alternative to null
  • Chapter 11: CompletableFuture: composable asynchronous programming
  • Chapter 12: New Date and Time API
  • Chapter 13: Thinking functionally
  • Chapter 14: Functional programming techniques
  • Chapter 15: Blending OOP and FP: comparing Java 8 and Scala
  • Chapter 16: Conclusions and "where next" for Java
  • Appendix A: Miscellaneous language updates
  • Appendix B: Miscellaneous library updates
  • Appendix C: Performing multiple operations in parallel on a Stream
  • Appendix D: Lambdas and JVM bytecode We will update the repository as we update the book. Stay tuned!

Make sure to have JDK8 installed

The latest binary can be found here: http://www.oracle.com/technetwork/java/javase/overview/java8-2100321.html

$ java -version

java version "1.8.0_05" Java(TM) SE Runtime Environment (build 1.8.0_05-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

You can download a preview version here: https://jdk8.java.net/

Compile/Run the examples

Using maven:

$ mvn compile

$ cd target/classes

$ java lambdasinaction/chap1/FilteringApples

Alternatively you can compile the files manually inside the directory src/main/java

You can also import the project in your favorite IDE: * In IntelliJ use "File->Open" menu and navigate to the folder where the project resides * In Eclipse use "File->Import->Existing Maven Projects" (also modify "Reduntant super interfaces" to report as Warnings instead of Errors * In Netbeans use "File->Open Project" menu

java8inaction's People

Contributors

java8 avatar jcabmora avatar jeffmorin avatar mariofusco avatar phillips1021 avatar raouldoc avatar romankagan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

java8inaction's Issues

BUILD FAILURE

I try to compile sources on Mac with Java 1.8.0_91

I get:
chojnasm-ml:Java8InAction chojnasm$ mvn compile
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for manning:lambdasinaction:jar:1.0
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-shade-plugin is missing. @ line 44, column 15
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building lambdasinaction 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ lambdasinaction ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ lambdasinaction ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 100 source files to /Users/chojnasm/Downloads/Java8InAction/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.558 s
[INFO] Finished at: 2017-02-27T14:27:45+00:00
[INFO] Final Memory: 9M/309M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project lambdasinaction: Fatal error compiling: invalid target release: 1.9 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Chapter 10 examples,I got error!

Can you run?I can not successfully run the OptionalMain.java.What went wrong,Here
public String getCarInsuranceName(Optional<Person> person) { return person.flatMap(Person::getCar) .flatMap(Car::getInsurance) .map(Insurance::getName) .orElse("Unknown"); }

I got NullPointException.The Exception:
image

Grouping class has two errors with undefined functions at line 36 and 41

private static Map<Dish.Type, Set> groupDishTagsByType() {
return menu.stream().collect(groupingBy(Dish::getType, flatMapping(dish -> dishTags.get( dish.getName() ).stream(), toSet())));
}
-> flatMapping shown as invalid and don't compile the class

private static Map<Dish.Type, List<Dish>> groupCaloricDishesByType() {

// return menu.stream().filter(dish -> dish.getCalories() > 500).collect(groupingBy(Dish::getType));
return menu.stream().collect(groupingBy(Dish::getType, filtering(dish -> dish.getCalories() > 500, toList())));
}
-> filtering shown as invalid and don't compile the class

Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>)

BestPriceFinder.java

public Stream<CompletableFuture> findPricesStream(String product) {
return shops.stream()
.map(shop -> CompletableFuture.supplyAsync(() -> shop.getPrice(product), executor))
.map(future -> future.thenApply(Quote::parse))
.map(future -> future.thenCompose(quote -> CompletableFuture.supplyAsync(() -> Discount.applyDiscount(quote), executor)));
}

the class PartitionPrimeNumbers has an error at line 36 and don't compile

public static boolean isPrime(List<Integer> primes, Integer candidate) {
    double candidateRoot = Math.sqrt((double) candidate);
    //return takeWhile(primes, i -> i <= candidateRoot).stream().noneMatch(i -> candidate % i == 0);
    return primes.stream().**takeWhile**(i -> i <= candidateRoot).noneMatch(i -> candidate % i == 0);
}

-> the function takeWhile is invalid or doesn't exists

有些方法找不到 Some method is undefined

class: lambdasinaction.chap10.OperationsWithOptional
line 15 and line 18
The method or(() -> {}) is undefined for the type Optional


class: lambdasinaction.chap10.OptionalMain
line 21
The method flatMap(Function<? super Optional,? extends Stream<? extends R>>) in the type Stream<Optional> is not applicable for the arguments (Optional::stream)


class: lambdasinaction.chap6.Grouping
line 36
The method flatMapping(( dish) -> {}, toSet()) is undefined for the type Grouping

line 41
The method filtering(( dish) -> {}, toList()) is undefined for the type Grouping


class: lambdasinaction.chap6.PartitionPrimeNumbers
line 36
The method takeWhile(( i) -> {}) is undefined for the type Stream

lambdasinaction code example Eorror In Chap3

I downloaded the lambdasinaction code example, but there are some errors.
For example:
The method flatMapping(( dish) -> {}, toSet()) is undefined for the type Grouping in chap3 package, The code: private static Map<Dish.Type, Set<String>> groupDishTagsByType() { return menu.stream().collect(groupingBy(Dish::getType, flatMapping(dish -> dishTags.get( dish.getName() ).stream(), toSet()))); }

And The method filtering(( dish) -> {}, toList()) is undefined for the type Grouping in chap3 package, The code private static Map<Dish.Type, List<Dish>> groupCaloricDishesByType() { return menu.stream().collect(groupingBy(Dish::getType, filtering(dish -> dish.getCalories() > 500, toList()))); }

When I practice, encountered a lot of errors . The method toList() 、groupingBy、comparing()....is undefined for the type TestStream.
By the way, i used jdk 1.8.0.171,Is it because the JDK version is too low ?

Stream() API support for Array

String[] stringArray = {"A,B","B,A","C,D","C,B","A,V"};
stringArray.stream() ------>> Cannot invoke stream() on the array type String[]

is it not good to have streaming on Array as well? :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.