返回一个Collector ,它产生应用于输入元素的双值函数的算术平均值。 如果没有元素,结果为0
Stream stream = Stream.of("10","20","30");double result = stream.collect(Collectors.averagingDouble((x -> Double.parseDouble(x))));System.out.println(result);
返回一个Collector ,它产生应用于输入元素的整数值函数的算术平均值。 如果没有元素,结果为0。
Stream stream = Stream.of("3","4","5");double result = stream.collect(Collectors.averagingInt(x-> Integer.parseInt(x)));System.out.println(result);
适应Collector进行额外的整理转换
Stream stream = Stream.of("Hello", "World"); List list = stream.collect(Collectors.collectingAndThen( Collectors.toList(), Collections:: unmodifiableList)); System.out.println(list);
返回一个Collector类型的接受元素,类型为T ,它计算输入元素的数量。 如果没有元素,结果为0
Stream stream = Stream.of("1","2","3","4");Long result = stream.collect(Collectors.counting());System.out.println(result);
返回Collector “由基团”上的类型的输入元件操作实现T ,根据分类功能分组元素,并且在返回的结果Map 。
Stream stream = Stream.of("apple","apple","pear","banana");Map map =stream.collect(Collectors.groupingBy( Function.identity(), Collectors.counting()));System.out.println(map);
适应一个 Collector类型的接受元件 U至类型的一个接受元件 T通过积累前应用映射函数到每个输入元素。
Stream stream = Stream.of( new Employee("joe",30), new Employee("lin",30));List names = stream.collect( Collectors.mapping(x->x.getName(),Collectors.toList()));System.out.println(names);
按照城市先分组后统计
List list = Arrays.asList( new Person("Beijing","zhangsan","zhangsan"), new Person("Shanghai","lisi", "lisi"));Map> map = list.stream().collect( groupingBy(Person::getCity, mapping(Person::getFirstName,toSet())));System.out.println(map);
返回一个Collector ,它根据Predicate对输入元素进行Predicate ,并将它们组织成Map
Stream stream = Stream.of("John","John","Anna","James");Map> map = stream.collect( Collectors.partitioningBy(x->x.length()>4));System.out.println(map);
返回一个Collector ,它在指定的Collector下执行其输入元素的BinaryOperator 。 结果被描述为Optional
Stream stream = Stream.of(new Employee("joe",30), new Employee("john",40)); Optional optEmployee = stream.collect(Collectors.reducing(BinaryOperator.maxBy(Comparator.comparing(Employee::getAge)))); if (optEmployee.isPresent()){ System.out.println(optEmployee.get().getAge()+","+optEmployee.get().getName());}
返回一个 Collector , double生产映射函数应用于每个输入元素,并返回结果值得汇总统计信息
List list = Arrays.asList(23.43,23.32,8.7);Stream stream = list.stream();DoubleSummaryStatistics doubleSummaryStatistics = stream.collect(Collectors.summarizingDouble(x->x));System.out.println(doubleSummaryStatistics);
返回一个Collector ,它产生应用于输入元素的整数值函数的和。 如果没有元素,结果为0。
List list = Arrays.asList(10,20,35); Stream integerStream = list.stream(); IntSummaryStatistics intSummaryStatistics = integerStream.collect(Collectors.summarizingInt(x->x)); System.out.println(intSummaryStatistics);
返回一个Collector ,按照遇到的顺序将输入元素累加到一个新的Collection中。 Collection由提供的工厂创建。
Stream stream = Stream.of("Hello", "World");Set set = stream.collect(Collectors.toCollection(TreeSet::new));System.out.println(set);
Stream stream = Stream.of( new Person("beijing","zhangsan","zhangsan"), new Person("shanghai","lisi","lisi") );Map map = stream.collect(Collectors.toConcurrentMap(Person::getCity, Person::getFirstName));System.out.println(map);
返回一个Collector ,它将输入元素List到一个新的List.
Stream stream = Stream.of( new Person("beijing","zhangsan","zhangsan"), new Person("shanghai","lisi","lisi"));List list = stream.map(x->x.getFirstName()).collect(Collectors.toList());
返回一个Collector ,将输入元素Set到一个新的Set
Stream stream = Stream.of( new Person("beijing","zhangsan","zhangsan"), new Person("shanghai","lisi","lisi"));Set set = stream.map(x->x.getCity()).collect(Collectors.toSet());System.out.println(set);
上面提到java.util.Collectors里的这些方法大多数里用到了参数BinaryOperator, 这是一个函数式接口,可以用做lambda表达式或者方法引用的赋值对象
@FunctionalInterfacepublic interface BinaryOpeator extends BiFunction{ }
Comparator comparator = (x, y) -> (x.compareTo(y)); BinaryOperator opMax = BinaryOperator.maxBy(comparator); System.out.println("Max:" + opMax.apply(5, 9)); System.out.println("Max:" + opMax.apply(6, 9)); BinaryOperator opMin = BinaryOperator.minBy(comparator); System.out.println("Min:" + opMin.apply(5, 6)); System.out.println("Min:" + opMin.apply(9, 6));
留言与评论(共有 0 条评论) “” |