diff --git a/pom.xml b/pom.xml index 10e5035e..37cbf712 100644 --- a/pom.xml +++ b/pom.xml @@ -37,8 +37,8 @@ maven-compiler-plugin 3.1 - 1.9 - 1.9 + 1.8 + 1.8 diff --git a/src/main/java/lambdasinaction/appa/Book.java b/src/main/java/lambdasinaction/appa/Book.java index beefd807..58c1fb38 100644 --- a/src/main/java/lambdasinaction/appa/Book.java +++ b/src/main/java/lambdasinaction/appa/Book.java @@ -9,7 +9,7 @@ public class Book { public static void main(String[] args) { Author[] authors = Book.class.getAnnotationsByType(Author.class); - Arrays.asList(authors).stream().forEach(a -> { + Arrays.stream(authors).forEach(a -> { System.out.println(a.name()); }); } diff --git a/src/main/java/lambdasinaction/appa/package-info.java b/src/main/java/lambdasinaction/appa/package-info.java new file mode 100644 index 00000000..49d282cb --- /dev/null +++ b/src/main/java/lambdasinaction/appa/package-info.java @@ -0,0 +1,9 @@ +/** + * @author renqun.yuan + * @date 2018/6/29 11:43 + */ +package lambdasinaction.appa; + +/* + * 注解的新特性 + */ \ No newline at end of file diff --git a/src/main/java/lambdasinaction/chap10/OperationsWithOptional.java b/src/main/java/lambdasinaction/chap10/OperationsWithOptional.java index d2940969..f1e2e19f 100644 --- a/src/main/java/lambdasinaction/chap10/OperationsWithOptional.java +++ b/src/main/java/lambdasinaction/chap10/OperationsWithOptional.java @@ -8,15 +8,15 @@ public class OperationsWithOptional { public static void main(String... args) { - System.out.println(max(of(3), of(5))); - System.out.println(max(empty(), of(5))); - - Optional opt1 = of(5); - Optional opt2 = opt1.or(() -> of(4)); - - System.out.println( - of(5).or(() -> of(4)) - ); +// System.out.println(max(of(3), of(5))); +// System.out.println(max(empty(), of(5))); +// +// Optional opt1 = of(5); +// Optional opt2 = opt1.or(() -> of(4)); +// +// System.out.println( +// of(5).or(() -> of(4)) +// ); } public static final Optional max(Optional i, Optional j) { diff --git a/src/main/java/lambdasinaction/chap10/OptionalMain.java b/src/main/java/lambdasinaction/chap10/OptionalMain.java index dcd97792..7910e1b8 100644 --- a/src/main/java/lambdasinaction/chap10/OptionalMain.java +++ b/src/main/java/lambdasinaction/chap10/OptionalMain.java @@ -13,12 +13,12 @@ public String getCarInsuranceName(Optional person) { .orElse("Unknown"); } - public Set getCarInsuranceNames(List persons) { - return persons.stream() - .map(Person::getCar) - .map(optCar -> optCar.flatMap(Car::getInsurance)) - .map(optInsurance -> optInsurance.map(Insurance::getName)) - .flatMap(Optional::stream) - .collect(toSet()); - } +// public Set getCarInsuranceNames(List persons) { +// return persons.stream() +// .map(Person::getCar) +// .map(optCar -> optCar.flatMap(Car::getInsurance)) +// .map(optInsurance -> optInsurance.map(Insurance::getName)) +// .flatMap(Optional::stream) +// .collect(toSet()); +// } } diff --git a/src/main/java/lambdasinaction/chap2/MeaningOfThis.java b/src/main/java/lambdasinaction/chap2/MeaningOfThis.java index 88dfb8e0..8c24e0e1 100644 --- a/src/main/java/lambdasinaction/chap2/MeaningOfThis.java +++ b/src/main/java/lambdasinaction/chap2/MeaningOfThis.java @@ -6,8 +6,10 @@ public class MeaningOfThis public void doIt() { int value = 6; + // 匿名类中this的含义, this代表该匿名类的属性 Runnable r = new Runnable(){ public final int value = 5; + @Override public void run(){ int value = 10; System.out.println(this.value); diff --git a/src/main/java/lambdasinaction/chap3/ExecuteAround.java b/src/main/java/lambdasinaction/chap3/ExecuteAround.java index 6c15fb6a..03daab4d 100644 --- a/src/main/java/lambdasinaction/chap3/ExecuteAround.java +++ b/src/main/java/lambdasinaction/chap3/ExecuteAround.java @@ -8,7 +8,7 @@ public static void main(String ...args) throws IOException{ // method we want to refactor to make more flexible String result = processFileLimited(); System.out.println(result); - + // 环绕执行的一种场景 System.out.println("---"); String oneLine = processFile((BufferedReader b) -> b.readLine()); diff --git a/src/main/java/lambdasinaction/chap4/StreamVsCollection.java b/src/main/java/lambdasinaction/chap4/StreamVsCollection.java index a72a0730..8c381943 100644 --- a/src/main/java/lambdasinaction/chap4/StreamVsCollection.java +++ b/src/main/java/lambdasinaction/chap4/StreamVsCollection.java @@ -4,7 +4,9 @@ import java.util.stream.*; import static java.util.stream.Collectors.toList; - +/** + * collection与stream的区别:collection存储和遍历数据,stream用来出来处理数据 + */ public class StreamVsCollection { public static void main(String...args){ diff --git a/src/main/java/lambdasinaction/chap5/BuildingStreams.java b/src/main/java/lambdasinaction/chap5/BuildingStreams.java index 15280a39..7cda212a 100644 --- a/src/main/java/lambdasinaction/chap5/BuildingStreams.java +++ b/src/main/java/lambdasinaction/chap5/BuildingStreams.java @@ -6,6 +6,14 @@ import java.nio.charset.Charset; import java.nio.file.*; +/** + * stream的创建方式 + * 1.Stream.of + * 2.Arrays.stream + * 3.collection的stream方法 + * 4.Stream.iterate + * 5.Stream.generate + */ public class BuildingStreams { public static void main(String...args) throws Exception{ diff --git a/src/main/java/lambdasinaction/chap6/CollectorHarness.java b/src/main/java/lambdasinaction/chap6/CollectorHarness.java index 8370be16..087ff8a3 100644 --- a/src/main/java/lambdasinaction/chap6/CollectorHarness.java +++ b/src/main/java/lambdasinaction/chap6/CollectorHarness.java @@ -6,7 +6,7 @@ public class CollectorHarness { public static void main(String[] args) { //System.out.println("Partitioning done in: " + execute(PartitionPrimeNumbers::partitionPrimes) + " msecs"); - System.out.println("Partitioning done in: " + execute(PartitionPrimeNumbers::partitionPrimesWithCustomCollector) + " msecs" ); +// System.out.println("Partitioning done in: " + execute(PartitionPrimeNumbers::partitionPrimesWithCustomCollector) + " msecs" ); } private static long execute(Consumer primePartitioner) { diff --git a/src/main/java/lambdasinaction/chap6/Grouping.java b/src/main/java/lambdasinaction/chap6/Grouping.java index 9105cc80..95c3e5b6 100644 --- a/src/main/java/lambdasinaction/chap6/Grouping.java +++ b/src/main/java/lambdasinaction/chap6/Grouping.java @@ -8,89 +8,89 @@ public class Grouping { - enum CaloricLevel { DIET, NORMAL, FAT }; - - public static void main(String ... args) { - System.out.println("Dishes grouped by type: " + groupDishesByType()); - System.out.println("Dish names grouped by type: " + groupDishNamesByType()); - System.out.println("Dish tags grouped by type: " + groupDishTagsByType()); - System.out.println("Caloric dishes grouped by type: " + groupCaloricDishesByType()); - System.out.println("Dishes grouped by caloric level: " + groupDishesByCaloricLevel()); - System.out.println("Dishes grouped by type and caloric level: " + groupDishedByTypeAndCaloricLevel()); - System.out.println("Count dishes in groups: " + countDishesInGroups()); - System.out.println("Most caloric dishes by type: " + mostCaloricDishesByType()); - System.out.println("Most caloric dishes by type: " + mostCaloricDishesByTypeWithoutOprionals()); - System.out.println("Sum calories by type: " + sumCaloriesByType()); - System.out.println("Caloric levels by type: " + caloricLevelsByType()); - } - - private static Map> groupDishesByType() { - return menu.stream().collect(groupingBy(Dish::getType)); - } - - private static Map> groupDishNamesByType() { - return menu.stream().collect(groupingBy(Dish::getType, mapping(Dish::getName, toList()))); - } - - private static Map> groupDishTagsByType() { - return menu.stream().collect(groupingBy(Dish::getType, flatMapping(dish -> dishTags.get( dish.getName() ).stream(), toSet()))); - } - - private static Map> 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()))); - } - - private static Map> groupDishesByCaloricLevel() { - return menu.stream().collect( - groupingBy(dish -> { - if (dish.getCalories() <= 400) return CaloricLevel.DIET; - else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL; - else return CaloricLevel.FAT; - } )); - } - - private static Map>> groupDishedByTypeAndCaloricLevel() { - return menu.stream().collect( - groupingBy(Dish::getType, - groupingBy((Dish dish) -> { - if (dish.getCalories() <= 400) return CaloricLevel.DIET; - else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL; - else return CaloricLevel.FAT; - } ) - ) - ); - } - - private static Map countDishesInGroups() { - return menu.stream().collect(groupingBy(Dish::getType, counting())); - } - - private static Map> mostCaloricDishesByType() { - return menu.stream().collect( - groupingBy(Dish::getType, - reducing((Dish d1, Dish d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2))); - } - - private static Map mostCaloricDishesByTypeWithoutOprionals() { - return menu.stream().collect( - groupingBy(Dish::getType, - collectingAndThen( - reducing((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2), - Optional::get))); - } - - private static Map sumCaloriesByType() { - return menu.stream().collect(groupingBy(Dish::getType, - summingInt(Dish::getCalories))); - } - - private static Map> caloricLevelsByType() { - return menu.stream().collect( - groupingBy(Dish::getType, mapping( - dish -> { if (dish.getCalories() <= 400) return CaloricLevel.DIET; - else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL; - else return CaloricLevel.FAT; }, - toSet() ))); - } +// enum CaloricLevel { DIET, NORMAL, FAT }; +// +// public static void main(String ... args) { +// System.out.println("Dishes grouped by type: " + groupDishesByType()); +// System.out.println("Dish names grouped by type: " + groupDishNamesByType()); +// System.out.println("Dish tags grouped by type: " + groupDishTagsByType()); +// System.out.println("Caloric dishes grouped by type: " + groupCaloricDishesByType()); +// System.out.println("Dishes grouped by caloric level: " + groupDishesByCaloricLevel()); +// System.out.println("Dishes grouped by type and caloric level: " + groupDishedByTypeAndCaloricLevel()); +// System.out.println("Count dishes in groups: " + countDishesInGroups()); +// System.out.println("Most caloric dishes by type: " + mostCaloricDishesByType()); +// System.out.println("Most caloric dishes by type: " + mostCaloricDishesByTypeWithoutOprionals()); +// System.out.println("Sum calories by type: " + sumCaloriesByType()); +// System.out.println("Caloric levels by type: " + caloricLevelsByType()); +// } +// +// private static Map> groupDishesByType() { +// return menu.stream().collect(groupingBy(Dish::getType)); +// } +// +// private static Map> groupDishNamesByType() { +// return menu.stream().collect(groupingBy(Dish::getType, mapping(Dish::getName, toList()))); +// } +// +// private static Map> groupDishTagsByType() { +// return menu.stream().collect(groupingBy(Dish::getType, flatMapping(dish -> dishTags.get( dish.getName() ).stream(), toSet()))); +// } +// +// private static Map> 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()))); +// } +// +// private static Map> groupDishesByCaloricLevel() { +// return menu.stream().collect( +// groupingBy(dish -> { +// if (dish.getCalories() <= 400) return CaloricLevel.DIET; +// else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL; +// else return CaloricLevel.FAT; +// } )); +// } +// +// private static Map>> groupDishedByTypeAndCaloricLevel() { +// return menu.stream().collect( +// groupingBy(Dish::getType, +// groupingBy((Dish dish) -> { +// if (dish.getCalories() <= 400) return CaloricLevel.DIET; +// else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL; +// else return CaloricLevel.FAT; +// } ) +// ) +// ); +// } +// +// private static Map countDishesInGroups() { +// return menu.stream().collect(groupingBy(Dish::getType, counting())); +// } +// +// private static Map> mostCaloricDishesByType() { +// return menu.stream().collect( +// groupingBy(Dish::getType, +// reducing((Dish d1, Dish d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2))); +// } +// +// private static Map mostCaloricDishesByTypeWithoutOprionals() { +// return menu.stream().collect( +// groupingBy(Dish::getType, +// collectingAndThen( +// reducing((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2), +// Optional::get))); +// } +// +// private static Map sumCaloriesByType() { +// return menu.stream().collect(groupingBy(Dish::getType, +// summingInt(Dish::getCalories))); +// } +// +// private static Map> caloricLevelsByType() { +// return menu.stream().collect( +// groupingBy(Dish::getType, mapping( +// dish -> { if (dish.getCalories() <= 400) return CaloricLevel.DIET; +// else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL; +// else return CaloricLevel.FAT; }, +// toSet() ))); +// } } diff --git a/src/main/java/lambdasinaction/chap6/PartitionPrimeNumbers.java b/src/main/java/lambdasinaction/chap6/PartitionPrimeNumbers.java index 69d7c4ca..9124ea1f 100644 --- a/src/main/java/lambdasinaction/chap6/PartitionPrimeNumbers.java +++ b/src/main/java/lambdasinaction/chap6/PartitionPrimeNumbers.java @@ -9,98 +9,98 @@ public class PartitionPrimeNumbers { - public static void main(String ... args) { - System.out.println("Numbers partitioned in prime and non-prime: " + partitionPrimes(100)); - System.out.println("Numbers partitioned in prime and non-prime: " + partitionPrimesWithCustomCollector(100)); +// public static void main(String ... args) { +// System.out.println("Numbers partitioned in prime and non-prime: " + partitionPrimes(100)); +// System.out.println("Numbers partitioned in prime and non-prime: " + partitionPrimesWithCustomCollector(100)); +// +// } +// +// public static Map> partitionPrimes(int n) { +// return IntStream.rangeClosed(2, n).boxed() +// .collect(partitioningBy(candidate -> isPrime(candidate))); +// } +// +// public static boolean isPrime(int candidate) { +// return IntStream.rangeClosed(2, candidate-1) +// .limit((long) Math.floor(Math.sqrt((double) candidate)) - 1) +// .noneMatch(i -> candidate % i == 0); +// } +// +// public static Map> partitionPrimesWithCustomCollector(int n) { +// return IntStream.rangeClosed(2, n).boxed().collect(new PrimeNumbersCollector()); +// } +// +//// public static boolean isPrime(List 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); +//// } +///* +// public static List takeWhile(List list, Predicate p) { +// int i = 0; +// for (A item : list) { +// if (!p.test(item)) { +// return list.subList(0, i); +// } +// i++; +// } +// return list; +// } +//*/ +// public static class PrimeNumbersCollector +// implements Collector>, Map>> { +// +// @Override +// public Supplier>> supplier() { +// return () -> new HashMap>() {{ +// put(true, new ArrayList()); +// put(false, new ArrayList()); +// }}; +// } +// +// @Override +// public BiConsumer>, Integer> accumulator() { +// return (Map> acc, Integer candidate) -> { +// acc.get( isPrime( acc.get(true), +// candidate) ) +// .add(candidate); +// }; +// } +// +// @Override +// public BinaryOperator>> combiner() { +// return (Map> map1, Map> map2) -> { +// map1.get(true).addAll(map2.get(true)); +// map1.get(false).addAll(map2.get(false)); +// return map1; +// }; +// } +// +// @Override +// public Function>, Map>> finisher() { +// return i -> i; +// } +// +// @Override +// public Set characteristics() { +// return Collections.unmodifiableSet(EnumSet.of(IDENTITY_FINISH)); +// } +// } - } - - public static Map> partitionPrimes(int n) { - return IntStream.rangeClosed(2, n).boxed() - .collect(partitioningBy(candidate -> isPrime(candidate))); - } - - public static boolean isPrime(int candidate) { - return IntStream.rangeClosed(2, candidate-1) - .limit((long) Math.floor(Math.sqrt((double) candidate)) - 1) - .noneMatch(i -> candidate % i == 0); - } - - public static Map> partitionPrimesWithCustomCollector(int n) { - return IntStream.rangeClosed(2, n).boxed().collect(new PrimeNumbersCollector()); - } - - public static boolean isPrime(List 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); - } -/* - public static List takeWhile(List list, Predicate p) { - int i = 0; - for (A item : list) { - if (!p.test(item)) { - return list.subList(0, i); - } - i++; - } - return list; - } -*/ - public static class PrimeNumbersCollector - implements Collector>, Map>> { - - @Override - public Supplier>> supplier() { - return () -> new HashMap>() {{ - put(true, new ArrayList()); - put(false, new ArrayList()); - }}; - } - - @Override - public BiConsumer>, Integer> accumulator() { - return (Map> acc, Integer candidate) -> { - acc.get( isPrime( acc.get(true), - candidate) ) - .add(candidate); - }; - } - - @Override - public BinaryOperator>> combiner() { - return (Map> map1, Map> map2) -> { - map1.get(true).addAll(map2.get(true)); - map1.get(false).addAll(map2.get(false)); - return map1; - }; - } - - @Override - public Function>, Map>> finisher() { - return i -> i; - } - - @Override - public Set characteristics() { - return Collections.unmodifiableSet(EnumSet.of(IDENTITY_FINISH)); - } - } - - public Map> partitionPrimesWithInlineCollector(int n) { - return Stream.iterate(2, i -> i + 1).limit(n) - .collect( - () -> new HashMap>() {{ - put(true, new ArrayList()); - put(false, new ArrayList()); - }}, - (acc, candidate) -> { - acc.get( isPrime(acc.get(true), candidate) ) - .add(candidate); - }, - (map1, map2) -> { - map1.get(true).addAll(map2.get(true)); - map1.get(false).addAll(map2.get(false)); - }); - } +// public Map> partitionPrimesWithInlineCollector(int n) { +// return Stream.iterate(2, i -> i + 1).limit(n) +// .collect( +// () -> new HashMap>() {{ +// put(true, new ArrayList()); +// put(false, new ArrayList()); +// }}, +// (acc, candidate) -> { +// acc.get( isPrime(acc.get(true), candidate) ) +// .add(candidate); +// }, +// (map1, map2) -> { +// map1.get(true).addAll(map2.get(true)); +// map1.get(false).addAll(map2.get(false)); +// }); +// } }