java8 / Java8InAction

MIT License
3.19k stars 2.26k forks source link

lambdasinaction code example Eorror In Chap3 #22

Closed 156235yuan closed 5 years ago

156235yuan commented 6 years ago

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 ?

bsbravo commented 6 years ago

This is because the source code was updated to Java 9.

takeWhile is a new method added to Stream interface in Java 9:

https://docs.oracle.com/javase/9/docs/api/java/util/stream/Stream.html#takeWhile-java.util.function.Predicate-

filtering and flatMapping where added to the Collectors interface:

https://docs.oracle.com/javase/9/docs/api/java/util/stream/Collectors.html#flatMapping-java.util.function.Function-java.util.stream.Collector-

https://docs.oracle.com/javase/9/docs/api/java/util/stream/Collectors.html#filtering-java.util.function.Predicate-java.util.stream.Collector-

So, update to JDK 9.

156235yuan commented 5 years ago

thank you,I see in pom.xml that the environment needs jdk9. but this book is jdk8Inaction. Well, i admit i was careless... by the way, i download jdk9. its no many jar,eg:rt.jar,resource.jar... so, i cannot use jdk9