Java8

使用流

2018-05-20  本文已影响2人  小鱼嘻嘻
筛选好,切片和匹配
List<Dish> meat = menu.stream()
.filter(dish -> dish.getType().equals("meat"))
.collect(Collectors.toList());
image.png
 List<Dish> meat = menu.stream().
            filter(dish -> dish.getType().equals("meat"))
            .distinct()
            .collect(Collectors.toList());
 List<Dish> meat = menu.stream().
            filter(dish -> dish.getType().equals("meat"))
            .distinct()
            .limit(1)
            .collect(Collectors.toList());
  List<Dish> meats = menu.stream().
            filter(dish -> dish.getType().equals("meat"))
            .distinct()
            .skip(1)
            .collect(Collectors.toList());
    List<Dish> twoMeats   = menu.stream()
            .filter(dish -> dish.getType().equals("meat"))
            .limit(2)
            .collect(Collectors.toList());
        List<String> names = menu.stream()
            .map(dish -> dish.getName())
            .collect(Collectors.toList());
 List<DishColor> dishColors = menu.stream()
            .flatMap(dish -> dish.getDishColors().stream())
            .collect(Collectors.toList());
 List<Integer> numbers = Arrays.asList(1,2,3,4,5);
        List<Integer> nums = numbers.stream().map(x -> x * x)
            .collect(Collectors.toList());

给定1,2,3和3,4返回(1,3)(1,4)(2,1)(2,4)(3,1)(3,4)

List<Integer> number1s = Arrays.asList(1, 2, 3);
        List<Integer> number2s = Arrays.asList(3, 4);
        number1s.stream().
            flatMap(x ->
                number2s.stream()
                    .map(y -> new int[] {x, y}))
            .collect(Collectors.toList());
查找,匹配和规约
 boolean isfind = menu.stream().anyMatch(dish -> dish.getType().equals("meat"));
 boolean allfind = menu.stream().allMatch(dish -> dish.getType().equals("meat"));
 boolean nonefind = menu.stream().noneMatch(dish -> dish.getType().equals("meat"));
 Optional<Dish> meat = menu.stream().filter(dish -> dish.getType().equals("meat")).findAny();
 Optional<Dish> meat = menu.stream().filter(dish -> dish.getType().equals("meat")).findFirst();
 Optional<Dish> meat = menu.stream().filter(dish -> dish.getType().equals("meat")).findAny();
        Dish dish = meat.get();
        System.out.println(dish);
        Dish orElse = meat.orElse(new Dish());
        System.out.println(orElse);
        boolean present = meat.isPresent();
        System.out.println(present);
        Integer reduce = numbers.stream().reduce(0, (x, y) -> x + y);
        Optional<Integer> reduce1 = numbers.stream().reduce( (x, y) -> x + y);
        Integer reduce2 = numbers.stream().reduce(0, Integer::sum);

reduce 接受两个参数
一个初始值
一个BinaryOperator<T>来将两个元素合并产生一个新的元素,
lambda (x,y)->x+y
求最大和最小值

      //max
        Optional<Integer> max = numbers.stream().reduce((x, y) -> x > y ? x : y);
        Optional<Integer> max1 = numbers.stream().reduce(Integer::max);
        //min
        Optional<Integer> min = numbers.stream().reduce((x, y) -> x <= y ? x : y);
        Optional<Integer> min1 = numbers.stream().reduce(Integer::min);
 Optional<Integer> reduce3 = menu.stream().map(dish1 -> 1).reduce(Integer::sum);
   long count = menu.stream().count();
使用数值范围等数值流

原始类型流特化
Java8引入三个原始类型流特化接口:IntStream LongStream DoubleStream分别将流中的元素特化为int ,long,double,从而避免装箱成本。

 IntStream intStream = menu.stream().mapToInt(Dish::getCategary);
 int count =  intStream.count();
Stream<Integer> boxed = intStream.boxed();
总计一下

这一章主要是介绍了流的一下常见用法,filter,map,flatMap,reduce

上一篇下一篇

猜你喜欢

热点阅读