Static factory method Java 8 Collectors class

  Taken combat >> << Java 8

Static factory method Collectors class
Factory Method Return Type For
toList List<T>  The flow of all items collected in a List
Example of use: List <Dish> dishes = menuStream.collect (toList ());
toSet Set<T> The flow of all items collected to a Set, remove duplicates
Example of use: Set <Dish> dishes = menuStream.collect (toSet ());
toCollection Collection<T> The flow of all items collected to the collection given supply source created

使用示例: Collection<Dish> dishes = menuStream.collect(toCollection(),ArrayList::new);

counting Long Calculation of the number of elementary stream
Example of use: long howManyDishes = menuStream.collect (counting ());
summingInt Integer An integer convection in the project properties summing

Example of use: int totalCalories = menuStream.collect (summingInt (Dish :: getCalories));

averagingInt Double Integer property item average value calculated flow

Example of use: double avgCalories = menuStream.collect (averagingInt (Dish :: getCalories));

summarizingInt IntSummaryStatistics

Collecting statistics on the attribute item Integer stream, such as maximum, minimum, sum and average

Example of use: IntSummaryStatistics menuStatistics = menuStream.collect (summarizingInt (Dish :: getCalories));

joining String Each item in the call connection convection generated string toString method

使用示例: String shortMenu = menuStream.map(Dish::getName).collect(joining(","));

maxBy Optional<T>

Wrapped in a flow element according to a given maximum Optional comparator selected, or if the stream is empty compared Optional.empty ()

使用示例: Optional<Dish> fattest = menuStream.collect(maxBy(comparingInt(Dish::getCalories)));

minBy Optional<T>

Optional smallest element of a package stream selected in accordance with the given comparator is empty, or if the stream was Optional.empty ()

使用示例: Optional<Dish> lightest = menuStream.collect(minBy(comparingInt(Dish::getCalories)));

reducing Type reduction operations generated

As an initial value from an accumulator starts, one by one using BinaryOperator elements in conjunction with the flow, so as to flow together around a single value

使用示例: int totalCalories = menuStream.collect(reducing(0, Dish::getCalories, Integer::sum));

collectingAndThen Type conversion function returns Another wrapping accumulator, its result using the transform function

使用示例: int howManyDishes = menuStream.collect(collectingAndThen(toList(), List::size));

groupingBy Map<K, List<T>>

根据项目的一个属性的值对流中的项目作问组,并将属性值作为结果 Map 的键

使用示例: Map<Dish.Type,List<Dish>> dishesByType = menuStream.collect(groupingBy(Dish::getType));

partitioningBy Map<Boolean,List<T>> 根据对流中每个项目应用谓词的结果来对项目进行分区

使用示例: Map<Boolean,List<Dish>> vegetarianDishes = menuStream.collect(partitioningBy(Dish::isVegetarian));

   

Guess you like

Origin www.cnblogs.com/Java-Script/p/11133696.html