|
经过一番苦战,终于把Java8 的绝大部分内容学完了,啃书还真心不容易呢
返回一个热量低于400的卡路里并按照热量升序获取菜肴名称
- public class Dish {
- private final String name;
- private final boolean vegetarian;
- private final int calories;
- private final Type type;
- public enum Type {
- MEAT, FISH, OTHER
- }
-
复制代码
结果:
[season fruit, prawns, rice]
- private List<Dish> menu = Arrays.asList(new Dish("pork", false, 800, Dish.Type.MEAT),
- new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT),
- new Dish("french fries", true, 530, Dish.Type.OTHER), new Dish("rice", true, 350, Dish.Type.OTHER),
- new Dish("season fruit", true, 120, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER),
- new Dish("prawns", false, 300, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH));;
复制代码
Java8之前
Java8 写法
stream 操作分为
数据源
中间操作
终端操作
数组-> Stream Arrays.stream(T[] arrays)
distinct-> toSet()
流操作的模式
过滤:filter(predicate), distinct(),limit(n),skip(n),
切片
查找:findFirst(),findAny()
匹配:allMatch(predicate),anyMatch(predicate),noneMatch(predicate)
映射:map(function),flatMap(function)
归约:reduce()
IntStream-> boxed()-> collect()
- private List<Dish> menu = Arrays.asList(new Dish("pork", false, 800, Dish.Type.MEAT),
- new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT),
- new Dish("french fries", true, 530, Dish.Type.OTHER), new Dish("rice", true, 350, Dish.Type.OTHER),
- new Dish("season fruit", true, 120, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER),
- new Dish("prawns", false, 300, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH));;
- @Test
- public void test00() {
- List<Dish> lowCaloricDishes = new ArrayList<>();
- for (Dish d : menu) {
- if (d.getCalories() < 400) {
- lowCaloricDishes.add(d);
- }
- }
- Collections.sort(lowCaloricDishes, new Comparator<Dish>() {
- public int compare(Dish d1, Dish d2) {
- return Integer.compare(d1.getCalories(), d2.getCalories());
- }
- });
- List<String> lowCaloricDishesName = new ArrayList<>();
- for (Dish d : lowCaloricDishes) {
- lowCaloricDishesName.add(d.getName());
- }
- System.out.println(lowCaloricDishesName);
- }
- @Test
- public void test01() {
- // 获取卡路里小于400 ,热量顺序排列获取菜肴名称
- List<String> list = menu.stream().filter(item -> item.getCalories() < 400)
- .sorted((i1, i2) -> Integer.compare(i1.getCalories(), i2.getCalories())).map(item -> item.getName())
- .collect(Collectors.toList());
- System.out.println(list);
- // 写法2, 使用辅助方法
- List<String> list2 = menu.stream().filter(item -> item.getCalories() < 400)
- .sorted(Comparator.comparing(Dish::getCalories)).map(Dish::getName).collect(Collectors.toList());
- System.out.println(list2);
- // 写法3,添加方法引用
- List<String> list3 = menu.stream().filter(Dish::isLose).sorted(Comparator.comparing(Dish::getCalories))
- .map(Dish::getName).collect(Collectors.toList());
- System.out.println(list3);
- }
- @Test
- public void test02() {
- Map<Type, List<Dish>> list = menu.stream().collect(Collectors.groupingBy(Dish::getType));
- System.out.println(list);
- }
- @Test
- public void test03() {
- // ["Hello","World"],你想要返回列表["H","e","l", "o","W","r","d"]。
- String[] strArr = { "Hello", "World" };
- List<String> collect = Arrays.stream(strArr).flatMap(str -> Arrays.asList(str.split("")).stream()).distinct()
- .collect(Collectors.toList());
- System.out.println(collect);
- List<String> collect2 = Arrays.asList(strArr).stream().map(str -> str.split("")).flatMap(Arrays::stream)
- .distinct().collect(Collectors.toList());
- System.out.println(collect2);
- }
- @Test
- public void test04() {
- // (1) 给定一个数字列表,如何返回一个由每个数的平方构成的列表呢?
- // 例如,给定[1, 2, 3, 4, 5],应该返回[1, 4, 9, 16, 25]
- int[] arr = { 1, 2, 3, 4, 5 };
- List<Integer> collect = Arrays.stream(arr).map(item -> item * item).boxed().collect(Collectors.toList());
- System.out.println(collect);
- }
- @Test
- public void test05() {
- // (2) 给定两个数字列表,如何返回所有的数对呢?
- // 例如,给定列表[1, 2, 3]和列表[3, 4],
- // 应该返回[(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)]。
- // 为简单起见,你可以用有两个元素的数组来代
- // 表数对。
- List<Integer> alist = Arrays.asList(1, 2, 3);
- List<Integer> blist = Arrays.asList(3, 4);
- List<int[]> collect = alist.stream().flatMap(item -> blist.stream().map(bitem -> new int[] { item, bitem }))
- .collect(Collectors.toList());
- for (int[] is : collect) {
- System.out.println(Arrays.toString(is));
- }
- System.out.println("====");
- // (3) 如何扩展前一个例子,只返回总和能被3整除的数对呢?例如(2, 4)和(3, 3)是可以的。
- List<int[]> collect2 = alist.stream().flatMap(
- item -> blist.stream().filter(bitem -> (item + bitem) % 3 == 0).map(bitem -> new int[] { item, bitem }))
- .collect(Collectors.toList());
- for (int[] is : collect2) {
- System.out.println(Arrays.toString(is));
- }
- }
- @Test
- public void test06() {
- IntStream stream = IntStream.of(1, 2, 3, 4, 5, 6, 7, 12);
- boolean allMatch = stream.allMatch(item -> item < 12);
- System.out.println(allMatch);
- stream = IntStream.of(1, 2, 3, 4, 5, 6, 7, 12);
- boolean anyMatch = stream.anyMatch(item -> item < 0);
- System.out.println(anyMatch);
- stream = IntStream.of(1, 2, 3, 4, 5, 6, 7, 12);
- boolean anyMatch2 = stream.noneMatch(item -> item < 0);
- System.out.println(anyMatch2);
- }
- @Test
- public void test07() {
- IntStream stream = IntStream.of(1, 2, 3, 4, 5, 6, 7);
- OptionalInt findFirst = stream.findFirst();
- findFirst.ifPresent(System.out::println);
- stream = IntStream.of(1, 2, 3, 4, 5, 6, 7);
- OptionalInt findFirst2 = stream.findAny();
- findFirst2.ifPresent(System.out::println);
- }
- @Test
- public void test08() {
- IntStream stream = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
- int reduce = stream.reduce(0, (i1, i2) -> i1 + i2);
- System.out.println(reduce);
- stream = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
- OptionalInt reduce2 = stream.reduce(Integer::sum);
- reduce2.ifPresent(System.out::println);
- }
-
- @Test
- public void test09() {
- IntStream stream = IntStream.of(1, 2, 32, 4, 5, 6, 7, 8, 9, 10);
- OptionalInt max = stream.max();
- System.out.println(max);
-
- List<Integer> asList = Arrays.asList(1,2,32,4,5,6,7,8,9,10);
- Integer reduce = asList.stream().reduce(0, Math:: max);
- System.out.println(reduce);
- }
-
- @Test
- public void test10() {
- IntStream stream = IntStream.of(1, 2, 32, 4, 5, 6, 7, 8, 9, 10);
- int sum = stream.sum();
- System.out.println(sum);
-
- List<Integer> asList = Arrays.asList(1,2,32,4,5,6,7,8,9,10);
- Integer reduce = asList.stream().mapToInt(i->i).sum();
- System.out.println(reduce);
-
- }
-
- @Test
- public void test11() {
- IntStream range = IntStream.rangeClosed(0, 100);
- int sum = range.sum();
- System.out.println(sum);
- }
-
- @Test
- public void test12() {
- IntStream rangeClosed = IntStream.rangeClosed(1, 100);
- rangeClosed.boxed().flatMap(a-> IntStream.rangeClosed(a, 100).filter(b-> Math.sqrt(a*a + b*b) % 1.0 ==0).mapToObj(b-> new int[] {a, b, (int)Math.sqrt(a*a + b*b)})).forEach(item->System.out.println(Arrays.toString(item)));
-
- }
-
- @Test
- public void test13() {
- Stream<Integer> iterate = Stream.iterate(1, i->i+1);
- List<Integer> collect = iterate.limit(10).collect(Collectors.toList());
- System.out.println(collect);
- }
-
- @Test
- public void test14() {
- IntStream stream = IntStream.of(1, 2, 32, 4, 5, 6, 7, 8, 9, 10);
- long count = stream.count();
- System.out.println(count);
-
- stream = IntStream.of(1, 2, 32, 4, 5, 6, 7, 8, 9, 10);
- Long collect = stream.boxed().collect(Collectors.counting());
- System.out.println(collect);
-
- stream = IntStream.of(1, 2, 32, 4, 5, 6, 7, 8, 9, 10);
- // OptionalInt max = stream.max();
- Optional<Integer> collect2 = stream.boxed().collect(Collectors.maxBy(Comparator.comparingInt(i->i)));
- System.out.println(collect2);
-
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|