| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,9 @@ | |||
| 9 | 9 | * Created by brian on 17/3/20. | |
| 10 | 10 | * 使用 MethodHandle 来访问祖类方法 | |
| 11 | 11 | * TODO 为什么打印出来的是 "i am father" 而不是 "i am grandfather"? | |
| 12 | + * 参考: | ||
| 13 | + * https://www.zhihu.com/question/57379777 | ||
| 14 | + * https://www.zhihu.com/question/40427344/answer/86545388 | ||
| 12 | 15 | */ | |
| 13 | 16 | public class GrandFatherCaller { | |
| 14 | 17 | class GrandFather { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,6 +11,12 @@ | |||
| 11 | 11 | ||
| 12 | 12 | ## steam API | |
| 13 | 13 | ||
| 14 | - - Dish: | ||
| 14 | + - Dish: 菜肴类,有一个静态属性的菜肴列表 | ||
| 15 | 15 | - StreamBasic: 指令式和函数式分别挑选低热量食物名 | |
| 16 | - - StreamOperation: 流操作:中间操作和终端操作 | ||
| 16 | + - StreamOperation: 流操作(中间操作和终端操作) | ||
| 17 | + | ||
| 18 | + 使用流 | ||
| 19 | + | ||
| 20 | + - Filtering: 筛选(谓词筛选:filter;筛选互异的元素:distinct;忽略头几个元素:limit;截短至指定长度:skip) | ||
| 21 | + - Mapping: 映射 | ||
| 22 | + - PuttingIntoPractice: 各种流操作的使用示例 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,9 @@ public static void main(String... args) { | |||
| 24 | 24 | ||
| 25 | 25 | } | |
| 26 | 26 | ||
| 27 | + /** | ||
| 28 | + * 指令式 | ||
| 29 | + */ | ||
| 27 | 30 | public static List<String> getLowCaloricDishesNamesInJava7(List<Dish> dishes) { | |
| 28 | 31 | List<Dish> lowCaloricDishes = new ArrayList<>(); | |
| 29 | 32 | for (Dish d : dishes) { | |
@@ -43,6 +46,9 @@ public int compare(Dish d1, Dish d2) { | |||
| 43 | 46 | return lowCaloricDishesName; | |
| 44 | 47 | } | |
| 45 | 48 | ||
| 49 | + /** | ||
| 50 | + * 函数式 | ||
| 51 | + */ | ||
| 46 | 52 | public static List<String> getLowCaloricDishesNamesInJava8(List<Dish> dishes) { | |
| 47 | 53 | return dishes.stream() | |
| 48 | 54 | .filter(d -> d.getCalories() < 400) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,5 +26,9 @@ public static void main(String[] args) { | |||
| 26 | 26 | .limit(3) | |
| 27 | 27 | .collect(Collectors.toList()); | |
| 28 | 28 | System.out.println(names); | |
| 29 | + | ||
| 30 | + //forEach 是一个返回 void 的终端操作 | ||
| 31 | + System.out.println("------forEach-------"); | ||
| 32 | + menu.stream().forEach(System.out::println); | ||
| 29 | 33 | } | |
| 30 | 34 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,11 +1,14 @@ | |||
| 1 | - package com.brianway.learning.java8.streamapi; | ||
| 1 | + package com.brianway.learning.java8.streamapi.practice; | ||
| 2 | 2 | ||
| 3 | 3 | import static java.util.Comparator.comparing; | |
| 4 | 4 | import static java.util.stream.Collectors.toList; | |
| 5 | 5 | ||
| 6 | 6 | import java.util.Arrays; | |
| 7 | 7 | import java.util.List; | |
| 8 | 8 | ||
| 9 | + /** | ||
| 10 | + * 各种流操作的使用示例 | ||
| 11 | + */ | ||
| 9 | 12 | public class PuttingIntoPractice { | |
| 10 | 13 | public static void main(String... args) { | |
| 11 | 14 | Trader raoul = new Trader("Raoul", "Cambridge"); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,4 @@ | |||
| 1 | - package com.brianway.learning.java8.streamapi; | ||
| 1 | + package com.brianway.learning.java8.streamapi.practice; | ||
| 2 | 2 | ||
| 3 | 3 | public class Trader { | |
| 4 | 4 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,4 @@ | |||
| 1 | - package com.brianway.learning.java8.streamapi; | ||
| 1 | + package com.brianway.learning.java8.streamapi.practice; | ||
| 2 | 2 | ||
| 3 | 3 | public class Transaction { | |
| 4 | 4 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,54 @@ | |||
| 1 | + package com.brianway.learning.java8.streamapi.usage; | ||
| 2 | + | ||
| 3 | + import com.brianway.learning.java8.streamapi.Dish; | ||
| 4 | + import static com.brianway.learning.java8.streamapi.Dish.menu; | ||
| 5 | + import static java.util.stream.Collectors.toList; | ||
| 6 | + | ||
| 7 | + import java.util.Arrays; | ||
| 8 | + import java.util.List; | ||
| 9 | + | ||
| 10 | + /** | ||
| 11 | + *筛选 | ||
| 12 | + * - 谓词筛选:filter | ||
| 13 | + * - 筛选互异的元素:distinct | ||
| 14 | + * - 忽略头几个元素:limit | ||
| 15 | + * - 截短至指定长度:skip | ||
| 16 | + */ | ||
| 17 | + public class Filtering { | ||
| 18 | + | ||
| 19 | + public static void main(String...args){ | ||
| 20 | + | ||
| 21 | + // Filtering with predicate | ||
| 22 | + List<Dish> vegetarianMenu = | ||
| 23 | + menu.stream() | ||
| 24 | + .filter(Dish::isVegetarian) | ||
| 25 | + .collect(toList()); | ||
| 26 | + | ||
| 27 | + vegetarianMenu.forEach(System.out::println); | ||
| 28 | + | ||
| 29 | + // Filtering unique elements | ||
| 30 | + List<Integer> numbers = Arrays.asList(1, 2, 1, 3, 3, 2, 4); | ||
| 31 | + numbers.stream() | ||
| 32 | + .filter(i -> i % 2 == 0) | ||
| 33 | + .distinct() | ||
| 34 | + .forEach(System.out::println); | ||
| 35 | + | ||
| 36 | + // Truncating a stream | ||
| 37 | + List<Dish> dishesLimit3 = | ||
| 38 | + menu.stream() | ||
| 39 | + .filter(d -> d.getCalories() > 300) | ||
| 40 | + .limit(3) | ||
| 41 | + .collect(toList()); | ||
| 42 | + | ||
| 43 | + dishesLimit3.forEach(System.out::println); | ||
| 44 | + | ||
| 45 | + // Skipping elements | ||
| 46 | + List<Dish> dishesSkip2 = | ||
| 47 | + menu.stream() | ||
| 48 | + .filter(d -> d.getCalories() > 300) | ||
| 49 | + .skip(2) | ||
| 50 | + .collect(toList()); | ||
| 51 | + | ||
| 52 | + dishesSkip2.forEach(System.out::println); | ||
| 53 | + } | ||
| 54 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,41 @@ | |||
| 1 | + package com.brianway.learning.java8.streamapi.usage; | ||
| 2 | + | ||
| 3 | + import com.brianway.learning.java8.streamapi.Dish; | ||
| 4 | + import static com.brianway.learning.java8.streamapi.Dish.menu; | ||
| 5 | + | ||
| 6 | + import java.util.Optional; | ||
| 7 | + | ||
| 8 | + /** | ||
| 9 | + * 查找和匹配 | ||
| 10 | + */ | ||
| 11 | + public class Finding { | ||
| 12 | + | ||
| 13 | + public static void main(String...args){ | ||
| 14 | + if(isVegetarianFriendlyMenu()){ | ||
| 15 | + System.out.println("Vegetarian friendly"); | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + System.out.println(isHealthyMenu()); | ||
| 19 | + System.out.println(isHealthyMenu2()); | ||
| 20 | + | ||
| 21 | + Optional<Dish> dish = findVegetarianDish(); | ||
| 22 | + dish.ifPresent(d -> System.out.println(d.getName())); | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + private static boolean isVegetarianFriendlyMenu(){ | ||
| 26 | + return menu.stream().anyMatch(Dish::isVegetarian); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + private static boolean isHealthyMenu(){ | ||
| 30 | + return menu.stream().allMatch(d -> d.getCalories() < 1000); | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + private static boolean isHealthyMenu2(){ | ||
| 34 | + return menu.stream().noneMatch(d -> d.getCalories() >= 1000); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + private static Optional<Dish> findVegetarianDish(){ | ||
| 38 | + return menu.stream().filter(Dish::isVegetarian).findAny(); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,6 @@ | |||
| 1 | - package com.brianway.learning.java8.streamapi; | ||
| 1 | + package com.brianway.learning.java8.streamapi.usage; | ||
| 2 | 2 | ||
| 3 | + import com.brianway.learning.java8.streamapi.Dish; | ||
| 3 | 4 | import static com.brianway.learning.java8.streamapi.Dish.menu; | |
| 4 | 5 | import static java.util.stream.Collectors.toList; | |
| 5 | 6 | ||
@@ -9,6 +10,7 @@ | |||
| 9 | 10 | /** | |
| 10 | 11 | * 使用流-映射 | |
| 11 | 12 | * 注意扁平映射 | |
| 13 | + * flatMap 的效果:各个数组不是分别映射成一个流,而是映射成 流的内容 | ||
| 12 | 14 | */ | |
| 13 | 15 | public class Mapping { | |
| 14 | 16 | ||
@@ -44,5 +46,15 @@ public static void main(String... args) { | |||
| 44 | 46 | .filter(pair -> (pair[0] + pair[1]) % 3 == 0) | |
| 45 | 47 | .collect(toList()); | |
| 46 | 48 | pairs.forEach(pair -> System.out.println("(" + pair[0] + ", " + pair[1] + ")")); | |
| 49 | + | ||
| 50 | + // flatMap | ||
| 51 | + List<int[]> pairs2 = numbers1.stream() | ||
| 52 | + .flatMap(i -> numbers2.stream() | ||
| 53 | + .filter(j -> (i + j) % 3 == 0) | ||
| 54 | + .map(j -> new int[] {i, j}) | ||
| 55 | + ) | ||
| 56 | + .collect(toList()); | ||
| 57 | + System.out.println("----------"); | ||
| 58 | + pairs2.forEach(pair -> System.out.println("(" + pair[0] + ", " + pair[1] + ")")); | ||
| 47 | 59 | } | |
| 48 | 60 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments