public final class Collectors extends Object
Collector实现,如积累元素的集合,总结元素根据不同的标准,
以下是使用预定义的收藏家来执行常见的可变的减排任务的例子:
// Accumulate names into a List
List<String> list = people.stream().map(Person::getName).collect(Collectors.toList());
// Accumulate names into a TreeSet
Set<String> set = people.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new));
// Convert elements to strings and concatenate them, separated by commas
String joined = things.stream()
.map(Object::toString)
.collect(Collectors.joining(", "));
// Compute sum of salaries of employee
int total = employees.stream()
.collect(Collectors.summingInt(Employee::getSalary)));
// Group employees by department
Map<Department, List<Employee>> byDept
= employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment));
// Compute sum of salaries by department
Map<Department, Integer> totalByDept
= employees.stream()
.collect(Collectors.groupingBy(Employee::getDepartment,
Collectors.summingInt(Employee::getSalary)));
// Partition students into passing and failing
Map<Boolean, List<Student>> passingFailing =
students.stream()
.collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD));
等
| Modifier and Type | Method and Description |
|---|---|
static <T> Collector<T,?,Double> |
averagingDouble(ToDoubleFunction<? super T> mapper)
返回一个
Collector产生一个双值函数应用于输入元素的算术平均值。
|
static <T> Collector<T,?,Double> |
averagingInt(ToIntFunction<? super T> mapper)
返回一个
Collector产生一个整数的值函数应用于输入元素的算术平均值。
|
static <T> Collector<T,?,Double> |
averagingLong(ToLongFunction<? super T> mapper)
返回一个
Collector生产长值函数应用于输入元素的算术平均值。
|
static <T,A,R,RR> Collector<T,A,RR> |
collectingAndThen(Collector<T,A,R> downstream, Function<R,RR> finisher)
适应
Collector执行一个额外的加工转化。
|
static <T> Collector<T,?,Long> |
counting()
返回一个
Collector接受型
T计数输入元素个数的元素。
|
static <T,K> Collector<T,?,Map<K,List<T>>> |
groupingBy(Function<? super T,? extends K> classifier)
返回一个
Collector实行“组”操作的输入元素类型
T,分组元素按一个分类函数,返回的结果在
Map。
|
static <T,K,A,D> Collector<T,?,Map<K,D>> |
groupingBy(Function<? super T,? extends K> classifier, Collector<? super T,A,D> downstream)
返回一个实现级联的“组”操作
Collector
T型输入元素,元素分组根据分类功能,然后执行还原操作对一个给定的键使用指定的下游
Collector关联的值。
|
static <T,K,D,A,M extends Map<K,D>> |
groupingBy(Function<? super T,? extends K> classifier, Supplier<M> mapFactory, Collector<? super T,A,D> downstream)
返回一个实现级联的“组”操作
Collector
T型输入元素,元素分组根据分类功能,然后执行还原操作对一个给定的键使用指定的下游
Collector关联的值。
|
static <T,K> Collector<T,?,ConcurrentMap<K,List<T>>> |
groupingByConcurrent(Function<? super T,? extends K> classifier)
返回一个并发
Collector实行“组”操作的输入元素类型
T,分组元素按照分类功能。
|
static <T,K,A,D> Collector<T,?,ConcurrentMap<K,D>> |
groupingByConcurrent(Function<? super T,? extends K> classifier, Collector<? super T,A,D> downstream)
返回一个
Collector实施并行级联”组的“
T型要素投入运作,分组元素按一个分类函数,然后执行还原操作对一个给定的键使用指定的下游
Collector关联的值。
|
static <T,K,A,D,M extends ConcurrentMap<K,D>> |
groupingByConcurrent(Function<? super T,? extends K> classifier, Supplier<M> mapFactory, Collector<? super T,A,D> downstream)
返回一个
Collector实施并行级联”组的“
T型要素投入运作,分组元素按一个分类函数,然后执行还原操作对一个给定的键使用指定的下游
Collector关联的值。
|
static Collector<CharSequence,?,String> |
joining()
返回一个
Collector,输入元素到一个
String,在遭遇订单。
|
static Collector<CharSequence,?,String> |
joining(CharSequence delimiter)
返回一个
Collector,输入元素,以指定的分隔符分隔,在遭遇订单。
|
static Collector<CharSequence,?,String> |
joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
返回一个
Collector,输入元素,以指定的分隔符分隔,用指定的前缀和后缀,在遭遇订单。
|
static <T,U,A,R> Collector<T,?,R> |
mapping(Function<? super T,? extends U> mapper, Collector<? super U,A,R> downstream)
适应
Collector接受型
U元素之一接受元素类型
T应用映射功能,每个输入元素之前的积累。
|
static <T> Collector<T,?,Optional<T>> |
maxBy(Comparator<? super T> comparator)
返回一个
Collector产生极大元根据给定的
Comparator,描述为一个
Optional<T>。
|
static <T> Collector<T,?,Optional<T>> |
minBy(Comparator<? super T> comparator)
返回一个
Collector产生最小的元素按照一定的
Comparator,描述为一个
Optional<T>。
|
static <T> Collector<T,?,Map<Boolean,List<T>>> |
partitioningBy(Predicate<? super T> predicate)
返回一个
Collector分区根据
Predicate输入元素,并将它们组织成一个
Map<Boolean, List<T>>。
|
static <T,D,A> Collector<T,?,Map<Boolean,D>> |
partitioningBy(Predicate<? super T> predicate, Collector<? super T,A,D> downstream)
返回一个
Collector分区的输入元素,根据
Predicate,降低值在每个分区根据另一
Collector,并将它们组织成一个
Map<Boolean, D>其值是下游减少的结果。
|
static <T> Collector<T,?,Optional<T>> |
reducing(BinaryOperator<T> op)
返回一个
Collector执行指定的
BinaryOperator下减少其投入要素。
|
static <T> Collector<T,?,T> |
reducing(T identity, BinaryOperator<T> op)
返回一个
Collector执行指定的
BinaryOperator下使用提供的身份的输入元素的减少。
|
static <T,U> Collector<T,?,U> |
reducing(U identity, Function<? super T,? extends U> mapper, BinaryOperator<U> op)
返回一个
Collector执行指定的映射功能和
BinaryOperator下减少其投入要素。
|
static <T> Collector<T,?,DoubleSummaryStatistics> |
summarizingDouble(ToDoubleFunction<? super T> mapper)
返回一个
Collector采用
double-producing映射功能,每个输入元素,并返回结果值汇总统计。
|
static <T> Collector<T,?,IntSummaryStatistics> |
summarizingInt(ToIntFunction<? super T> mapper)
返回一个
Collector采用
int-producing映射功能,每个输入元素,并返回结果值汇总统计。
|
static <T> Collector<T,?,LongSummaryStatistics> |
summarizingLong(ToLongFunction<? super T> mapper)
返回一个
Collector采用
long-producing映射功能,每个输入元素,并返回结果值汇总统计。
|
static <T> Collector<T,?,Double> |
summingDouble(ToDoubleFunction<? super T> mapper)
返回一个
Collector产生一个双值函数应用于投入要素的总和。
|
static <T> Collector<T,?,Integer> |
summingInt(ToIntFunction<? super T> mapper)
返回一个
Collector产生一个整数的函数应用于输入元素的总和。
|
static <T> Collector<T,?,Long> |
summingLong(ToLongFunction<? super T> mapper)
返回一个
Collector生产长值函数应用于投入要素的总和。
|
static <T,C extends Collection<T>> |
toCollection(Supplier<C> collectionFactory)
返回一个
Collector积累输入元素到一个新的
Collection,在遭遇订单。
|
static <T,K,U> Collector<T,?,ConcurrentMap<K,U>> |
toConcurrentMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
返回一个并发
Collector积累成
ConcurrentMap元素的键和值是应用提供的函数映射到输入元素的结果。
|
static <T,K,U> Collector<T,?,ConcurrentMap<K,U>> |
toConcurrentMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction)
返回一个并发
Collector积累成
ConcurrentMap元素的键和值是应用提供的函数映射到输入元素的结果。
|
static <T,K,U,M extends ConcurrentMap<K,U>> |
toConcurrentMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier)
返回一个并发
Collector积累成
ConcurrentMap元素的键和值是应用提供的函数映射到输入元素的结果。
|
static <T> Collector<T,?,List<T>> |
toList()
返回一个
Collector积累输入元素到一个新的
List。
|
static <T,K,U> Collector<T,?,Map<K,U>> |
toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
返回一个
Collector积累成一个
Map元素的键和值是应用提供的函数映射到输入元素的结果。
|
static <T,K,U> Collector<T,?,Map<K,U>> |
toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction)
返回一个
Collector积累成一个
Map元素的键和值是应用提供的函数映射到输入元素的结果。
|
static <T,K,U,M extends Map<K,U>> |
toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier)
返回一个
Collector积累成一个
Map元素的键和值是应用提供的函数映射到输入元素的结果。
|
static <T> Collector<T,?,Set<T>> |
toSet()
返回一个
Collector积累输入元素到一个新的
Set。
|
public static <T,C extends Collection<T>> Collector<T,?,C> toCollection(Supplier<C> collectionFactory)
Collector积累输入元素到一个新的
Collection,在遭遇订单。的
Collection所提供的工厂创建。
T -输入元素的类型
C -产生的
Collection类型
collectionFactory -
Supplier返回一个新的适当类型的空
Collection
Collector收集所有输入要素为
Collection,在遭遇订单
public static <T> Collector<T,?,List<T>> toList()
Collector积累输入元素到一个新的
List。有没有保证的类型、易变性、可串行化的,或
List线程安全返回;如果在返回
List更多的控制是必要的,使用
toCollection(Supplier)。
T -输入元素的类型
Collector收集所有输入要素为
List,在遭遇订单
public static <T> Collector<T,?,Set<T>> toSet()
Collector积累输入元素到一个新的
Set。有没有保证的类型、易变性、可串行化的,或
Set线程安全返回;如果在返回
Set是需要更多的控制,使用
toCollection(Supplier)。
这是一个unordered收集器。
T -输入元素的类型
Collector收集所有输入要素为
Set
public static Collector<CharSequence,?,String> joining()
Collector,输入元素到一个
String,在遭遇订单。
Collector,输入元素到一个
String,在遭遇订单
public static Collector<CharSequence,?,String> joining(CharSequence delimiter)
Collector,输入元素,以指定的分隔符分隔,在遭遇订单。
delimiter -分隔符用于每个元素之间
Collector这将字符序列元素,以指定的分隔符分隔,在遭遇订单
public static Collector<CharSequence,?,String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
Collector,输入元素,以指定的分隔符分隔,用指定的前缀和后缀,在遭遇订单。
delimiter -分隔符用于每个元素之间
prefix -字符用于在连接结果开始的序列
suffix -字符用于在连接结果的序列
Collector这将字符序列元素,以指定的分隔符分隔,在遭遇订单
public static <T,U,A,R> Collector<T,?,R> mapping(Function<? super T,? extends U> mapper, Collector<? super U,A,R> downstream)
Collector接受型
U元素之一接受元素类型
T应用映射功能,每个输入元素之前的积累。
mapping()收藏家是最有用的使用时,在一个多层次的减少,如一
groupingBy或
partitioningBy下游。例如,给定的流
Person,积集上的名字在每个城市:
Map<City, Set<String>> lastNamesByCity
= people.stream().collect(groupingBy(Person::getCity,
mapping(Person::getLastName, toSet())));
T -输入元素的类型
U -下游收藏家接受元素类型
A -下游集电极中间堆积型
R -集电极结果类型
mapper -函数被应用于输入元素
downstream -集电极将接受映射值
public static <T,A,R,RR> Collector<T,A,RR> collectingAndThen(Collector<T,A,R> downstream, Function<R,RR> finisher)
Collector执行一个额外的加工转化。例如,一个能适应
toList()收藏家总是产生一个不可变列表:
List<String> people
= people.stream().collect(collectingAndThen(toList(), Collections::unmodifiableList));
T -输入元素的类型
A -下游集电极中间堆积型
R -下游集结果类型
RR -结果集的结果类型
downstream -集电极
finisher -一个函数被应用到下游的收藏家的最终结果
public static <T> Collector<T,?,Long> counting()
Collector接受型
T计数输入元素个数的元素。如果没有元素是存在的,结果是0。
reducing(0L, e -> 1L, Long::sum)
T -输入元素的类型
Collector计数输入元素
public static <T> Collector<T,?,Optional<T>> minBy(Comparator<? super T> comparator)
Collector产生最小的元素按照一定的
Comparator,描述为一个
Optional<T>。
reducing(BinaryOperator.minBy(comparator))
T -输入元素的类型
comparator -
Comparator比较元素
Collector产生最小值
public static <T> Collector<T,?,Optional<T>> maxBy(Comparator<? super T> comparator)
Collector产生极大元根据给定的
Comparator,描述为一个
Optional<T>。
reducing(BinaryOperator.maxBy(comparator))
T -输入元素的类型
comparator -
Comparator比较元素
Collector产生最大的价值
public static <T> Collector<T,?,Integer> summingInt(ToIntFunction<? super T> mapper)
Collector产生一个整数的函数应用于输入元素的总和。如果没有元素是存在的,结果是0。
T -输入元素的类型
mapper -功能提取的属性归纳
Collector产生派生属性的总和
public static <T> Collector<T,?,Long> summingLong(ToLongFunction<? super T> mapper)
Collector生产长值函数应用于投入要素的总和。如果没有元素是存在的,结果是0。
T -输入元素的类型
mapper -功能提取的属性归纳
Collector产生派生属性的总和
public static <T> Collector<T,?,Double> summingDouble(ToDoubleFunction<? super T> mapper)
Collector产生一个双值函数应用于投入要素的总和。如果没有元素是存在的,结果是0。
返回的金额可能会有所不同,这取决于记录的值的顺序,由于累积的四舍五入误差增加不同大小的值。通过增加绝对幅度排序的值往往会产生更准确的结果。如果任何记录的值是一个NaN或金额在任何一点的NaN然后总额将NaN。
T -输入元素的类型
mapper -功能提取的属性归纳
Collector产生派生属性的总和
public static <T> Collector<T,?,Double> averagingInt(ToIntFunction<? super T> mapper)
Collector产生一个整数的值函数应用于输入元素的算术平均值。如果没有元素是存在的,结果是0。
T -输入元素的类型
mapper -功能提取的属性归纳
Collector产生派生属性的总和
public static <T> Collector<T,?,Double> averagingLong(ToLongFunction<? super T> mapper)
Collector生产长值函数应用于输入元素的算术平均值。如果没有元素是存在的,结果是0。
T -输入元素的类型
mapper -功能提取的属性归纳
Collector产生派生属性的总和
public static <T> Collector<T,?,Double> averagingDouble(ToDoubleFunction<? super T> mapper)
Collector产生一个双值函数应用于输入元素的算术平均值。如果没有元素是存在的,结果是0。
返回的平均值可能会有所不同,这取决于记录的值的顺序,由于累积的四舍五入误差增加不同大小的值。通过增加绝对幅度排序的值往往会产生更准确的结果。如果任何记录的值是一个NaN或金额在任何一点的NaN那么平均将NaN。
double格式可以代表所有的连续整数的范围2到2
五十三
五十三。如果管道已超过2
五十三值,在平均计算的除数在2
五十三饱和,导致额外的数值误差。
T -输入元素的类型
mapper -功能提取的属性归纳
Collector产生派生属性的总和
public static <T> Collector<T,?,T> reducing(T identity, BinaryOperator<T> op)
Collector执行指定的
BinaryOperator下使用提供的身份的输入元素的减少。
reducing()收藏家是最有用的当使用多层次的减少,对
groupingBy或
partitioningBy下游。在流上执行一个简单的减少,而不是使用
Stream.reduce(Object, BinaryOperator) }。
T -输入和输出单元类型减少
identity -减少的标识值(同时,价值,回来时没有输入元素)
op -一个用于减少输入元素
BinaryOperator<T>
Collector进行还原操作
reducing(BinaryOperator),
reducing(Object, Function, BinaryOperator)
public static <T> Collector<T,?,Optional<T>> reducing(BinaryOperator<T> op)
Collector执行指定的
BinaryOperator下减少其投入要素。结果被描述为一个
Optional<T>。
reducing()收藏家是最有用的当使用多层次的减少,对
groupingBy或
partitioningBy下游。在流上执行一个简单的减少,而不是使用
Stream.reduce(BinaryOperator)。
例如,给定的流Person,计算每个城市最高的人:
Comparator<Person> byHeight = Comparator.comparing(Person::getHeight);
Map<City, Person> tallestByCity
= people.stream().collect(groupingBy(Person::getCity, reducing(BinaryOperator.maxBy(byHeight))));
T -输入和输出单元类型减少
op -一个用于减少输入元素
BinaryOperator<T>
Collector进行还原操作
reducing(Object, BinaryOperator),
reducing(Object, Function, BinaryOperator)
public static <T,U> Collector<T,?,U> reducing(U identity, Function<? super T,? extends U> mapper, BinaryOperator<U> op)
reducing()收藏家是最有用的当使用多层次的减少,对
groupingBy或
partitioningBy下游。执行一个简单的Map,减少流,使用
Stream.map(Function)和
Stream.reduce(Object, BinaryOperator)相反。
例如,给定的流Person,计算每个城市居民最长的姓氏:
Comparator<String> byLength = Comparator.comparing(String::length);
Map<City, String> longestLastNameByCity
= people.stream().collect(groupingBy(Person::getCity,
reducing(Person::getLastName, BinaryOperator.maxBy(byLength))));
T -输入元素的类型
U -映射的值的类型
identity -减少的标识值(同时,价值,回来时没有输入元素)
mapper -映射功能适用于每一个输入值
op -一个用于减少映射值
BinaryOperator<U>
Collector减少操作
reducing(Object, BinaryOperator),
reducing(BinaryOperator)
public static <T,K> Collector<T,?,Map<K,List<T>>> groupingBy(Function<? super T,? extends K> classifier)
Collector实行“组”操作的输入元素类型
T,分组元素按一个分类函数,返回的结果在
Map。
分类函数映射元素的一些关键型K。集热器产生的关键是Map<K, List<T>>应用分类功能的输入元素的值,其对应的值是包含输入元素映射到相关的关键作用下Lists分类。
有没有保证的类型、易变性、可串行化的,或Map或List对象的线程安全返回。
groupingBy(classifier, toList());
Collector不同步。平行流管道的
combiner功能通过合并从一个Map到另一个键,它可以是一个昂贵的操作。如果在元素出现在结果
Map收藏家为了保护不是必需的,使用
groupingByConcurrent(Function)可以提供更好的并行性能。
T -输入元素的类型
K -键的类型
classifier的分类器函数映射的输入元素的键
Collector操作
groupingBy(Function, Collector),
groupingBy(Function, Supplier, Collector),
groupingByConcurrent(Function)
public static <T,K,A,D> Collector<T,?,Map<K,D>> groupingBy(Function<? super T,? extends K> classifier, Collector<? super T,A,D> downstream)
Collector
T型输入元素,元素分组根据分类功能,然后执行还原操作对一个给定的键使用指定的下游
Collector关联的值。
分类函数映射元素的一些关键型K。下游的收藏家对T元素类型和产生的结果D型。由此产生的集热器产生的Map<K, D>。
有没有保证的类型、易变性、可串行化的,或Map线程安全返回。
例如,计算每个城市的人的最后一个名称的集合:
Map<City, Set<String>> namesByCity
= people.stream().collect(groupingBy(Person::getCity,
mapping(Person::getLastName, toSet())));
Collector不同步。平行流管道的
combiner功能通过合并从一个Map到另一个键,它可以是一个昂贵的操作。如果在元素介绍到下游的收藏家为了保护不是必需的,使用
groupingByConcurrent(Function, Collector)可以提供更好的并行性能。
T -输入元素的类型
K -键的类型
A -下游集电极中间堆积型
D -下游减少的结果类型
classifier -分类器函数映射的输入元素的键
downstream -实施下游减少
Collector
Collector级联操作
groupingBy(Function),
groupingBy(Function, Supplier, Collector),
groupingByConcurrent(Function, Collector)
public static <T,K,D,A,M extends Map<K,D>> Collector<T,?,M> groupingBy(Function<? super T,? extends K> classifier, Supplier<M> mapFactory, Collector<? super T,A,D> downstream)
Collector
T型输入元素,元素分组根据分类功能,然后执行还原操作对一个给定的键使用指定的下游
Collector关联的值。由集热器产生的
Map与供应厂创建功能。
分类函数映射元素的一些关键型K。下游的收藏家对T元素类型和产生的结果D型。由此产生的集热器产生的Map<K, D>。
例如,计算每个城市的人的最后一个名称的集合,其中城市名称是排序的:
Map<City, Set<String>> namesByCity
= people.stream().collect(groupingBy(Person::getCity, TreeMap::new,
mapping(Person::getLastName, toSet())));
Collector不同步。平行流管道的
combiner功能通过合并从一个Map到另一个键,它可以是一个昂贵的操作。如果在元素介绍到下游的收藏家为了保护不是必需的,使用
groupingByConcurrent(Function, Supplier, Collector)可以提供更好的并行性能。
T -输入元素的类型
K -键的类型
A -下游集电极中间堆积型
D -下游减少的结果类型
M -产生的
Map类型
classifier -分类器函数映射的输入元素的键
downstream -实施下游减少
Collector
mapFactory -一个函数,在调用时,会产生一个新的所需类型的空
Map
Collector级联操作
groupingBy(Function, Collector),
groupingBy(Function),
groupingByConcurrent(Function, Supplier, Collector)
public static <T,K> Collector<T,?,ConcurrentMap<K,List<T>>> groupingByConcurrent(Function<? super T,? extends K> classifier)
Collector实行“组”操作的输入元素类型
T,分组元素按照分类功能。
这是一个concurrent和unordered收集器。
分类函数映射元素的一些关键型K。集热器产生的关键是ConcurrentMap<K, List<T>>应用分类功能的输入元素的值,其对应的值是包含输入元素映射到相关的关键作用下Lists分类。
有类型,没有保证的可变性,或可串行化的Map或List对象返回的对象,或List线程安全返回。
groupingByConcurrent(classifier, toList());
T -输入元素的类型
K -键的类型
classifier -分类器函数映射的输入元素的键
Collector手术实施组
groupingBy(Function),
groupingByConcurrent(Function, Collector),
groupingByConcurrent(Function, Supplier, Collector)
public static <T,K,A,D> Collector<T,?,ConcurrentMap<K,D>> groupingByConcurrent(Function<? super T,? extends K> classifier, Collector<? super T,A,D> downstream)
Collector实施并行级联”组的“
T型要素投入运作,分组元素按一个分类函数,然后执行还原操作对一个给定的键使用指定的下游
Collector关联的值。
这是一个concurrent和unordered收集器。
分类函数映射元素的一些关键型K。下游的收藏家对T元素类型和产生的结果D型。由此产生的集热器产生的Map<K, D>。
例如,计算每个城市的人的最后一个名称的集合,其中城市名称是排序的:
ConcurrentMap<City, Set<String>> namesByCity
= people.stream().collect(groupingByConcurrent(Person::getCity,
mapping(Person::getLastName, toSet())));
T -输入元素的类型
K -键的类型
A -下游集电极中间堆积型
D -下游减少的结果类型
classifier -分类器函数映射的输入元素的键
downstream -实施下游减少
Collector
Collector手术实施梯级组
groupingBy(Function, Collector),
groupingByConcurrent(Function),
groupingByConcurrent(Function, Supplier, Collector)
public static <T,K,A,D,M extends ConcurrentMap<K,D>> Collector<T,?,M> groupingByConcurrent(Function<? super T,? extends K> classifier, Supplier<M> mapFactory, Collector<? super T,A,D> downstream)
Collector实施并行级联”组的“
T型要素投入运作,分组元素按一个分类函数,然后执行还原操作对一个给定的键使用指定的下游
Collector关联的值。由集热器产生的
ConcurrentMap与供应厂创建功能。
这是一个concurrent和unordered收集器。
分类函数映射元素的一些关键型K。下游的收藏家对T元素类型和产生的结果D型。由此产生的集热器产生的Map<K, D>。
例如,计算每个城市的人的最后一个名称的集合,其中城市名称是排序的:
ConcurrentMap<City, Set<String>> namesByCity
= people.stream().collect(groupingBy(Person::getCity, ConcurrentSkipListMap::new,
mapping(Person::getLastName, toSet())));
T -输入元素的类型
K -键的类型
A -下游集电极中间堆积型
D -下游减少的结果类型
M -产生的
ConcurrentMap类型
classifier -分类器函数映射的输入元素的键
downstream -实施下游减少
Collector
mapFactory -一个函数,在调用时,产生一个新的空
ConcurrentMap所需类型的
Collector手术实施梯级组
groupingByConcurrent(Function),
groupingByConcurrent(Function, Collector),
groupingBy(Function, Supplier, Collector)
public static <T> Collector<T,?,Map<Boolean,List<T>>> partitioningBy(Predicate<? super T> predicate)
Collector分区根据
Predicate输入元素,并将它们组织成一个
Map<Boolean, List<T>>。有没有保证的类型、易变性、可串行化的,或
Map线程安全返回。
T -输入元素的类型
predicate -用于分类输入元素的谓词
Collector
partitioningBy(Predicate, Collector)
public static <T,D,A> Collector<T,?,Map<Boolean,D>> partitioningBy(Predicate<? super T> predicate, Collector<? super T,A,D> downstream)
Collector分区的输入元素,根据
Predicate,降低值在每个分区根据另一
Collector,并将它们组织成一个
Map<Boolean, D>其值是下游减少的结果。
有没有保证的类型、易变性、可串行化的,或Map线程安全返回。
T -输入元素的类型
A -下游集电极中间堆积型
D -下游减少的结果类型
predicate -用于分类输入元素的谓词
downstream -实施下游减少
Collector
Collector
partitioningBy(Predicate)
public static <T,K,U> Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
Collector积累成一个
Map元素的键和值是应用提供的函数映射到输入元素的结果。
如果映射键包含重复的(根据Object.equals(Object)),一个IllegalStateException扔当集合进行操作。如果映射键可能有重复,使用toMap(Function, Function, BinaryOperator)相反。
Function.identity()可能有帮助。例如,下面的生产
Map映射的学生平均成绩:
Map<Student, Double> studentToGPA
students.stream().collect(toMap(Functions.identity(),
student -> computeGPA(student)));
和以下产生一个唯一的标识符来学生
Map映射:
Map<String, Student> studentIdToStudent
students.stream().collect(toMap(Student::getId,
Functions.identity());
Collector不同步。平行流管道的
combiner功能通过合并从一个Map到另一个键,它可以是一个昂贵的操作。如果它不是必需的,结果是插入在遭遇订单
Map,使用
toConcurrentMap(Function, Function)可以提供更好的并行性能。
T -输入元素的类型
K -关键的映射函数的输出类型
U -值映射函数的输出类型
keyMapper -映射函数来产生密钥
valueMapper -映射函数产生的值
Collector收集到一个
Map元素的键和值的输入元素运用映射函数的结果
toMap(Function, Function, BinaryOperator),
toMap(Function, Function, BinaryOperator, Supplier),
toConcurrentMap(Function, Function)
public static <T,K,U> Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction)
Collector积累成一个
Map元素的键和值是应用提供的函数映射到输入元素的结果。
如果映射键包含重复的(根据Object.equals(Object)),价值映射函数应用于每个元素,并对结果进行合并,使用提供的合并功能。
toMap仅仅使用合并功能,把无条件的,但你可以写的更灵活的合并政策。例如,如果你有一股
Person,和你想制造一个“电话簿”的名称到地址的映射,但它是可能的,两个人都有相同的名字,你可以做如下优雅地处理这些冲突,并产生一个
Map名称映射到一个地址链接列表:
Map<String, String> phoneBook
people.stream().collect(toMap(Person::getName,
Person::getAddress,
(s, a) -> s + ", " + a));
Collector不同步。平行流管道的
combiner功能通过合并从一个Map到另一个键,它可以是一个昂贵的操作。如果它不是必需的,结果合并成在遇到令
Map,使用
toConcurrentMap(Function, Function, BinaryOperator)可以提供更好的并行性能。
T -输入元素的类型
K -关键的映射函数的输出类型
U -值映射函数的输出类型
keyMapper -映射函数来产生密钥
valueMapper -映射函数产生的值
mergeFunction -合并功能,用于解决具有相同的键相关联的值之间的碰撞,提供
Map.merge(Object, Object, BiFunction)
Collector收集元素为
Map的关键是应用一个键映射功能,输入元素的结果,其值将值映射函数的所有输入的元素等于核心并使用合并功能的结果
toMap(Function, Function),
toMap(Function, Function, BinaryOperator, Supplier),
toConcurrentMap(Function, Function, BinaryOperator)
public static <T,K,U,M extends Map<K,U>> Collector<T,?,M> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier)
Collector积累成一个
Map元素的键和值是应用提供的函数映射到输入元素的结果。
如果映射键包含重复的(根据Object.equals(Object)),价值映射函数应用于每个元素,并对结果进行合并,使用提供的合并功能。通过提供的Map供应商创建功能。
Collector不同步。平行流管道的
combiner功能通过合并从一个Map到另一个键,它可以是一个昂贵的操作。如果它不是必需的,结果合并成在遇到令
Map,使用
toConcurrentMap(Function, Function, BinaryOperator, Supplier)可以提供更好的并行性能。
T -输入元素的类型
K -关键的映射函数的输出类型
U -值映射函数的输出类型
M -产生的
Map类型
keyMapper -映射函数来产生密钥
valueMapper -映射函数产生的值
mergeFunction -合并功能,用于解决具有相同的键相关联的值之间的碰撞,提供
Map.merge(Object, Object, BiFunction)
mapSupplier -一个函数返回一个新的、空的
Map,结果将被插入
Collector收集元素为
Map的关键是应用一个键映射功能,输入元素的结果,其值将值映射函数的所有输入的元素等于核心并使用合并功能的结果
toMap(Function, Function),
toMap(Function, Function, BinaryOperator),
toConcurrentMap(Function, Function, BinaryOperator, Supplier)
public static <T,K,U> Collector<T,?,ConcurrentMap<K,U>> toConcurrentMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
Collector积累成
ConcurrentMap元素的键和值是应用提供的函数映射到输入元素的结果。
如果映射键包含重复的(根据Object.equals(Object)),一个IllegalStateException扔当集合进行操作。如果映射键可能有重复,使用toConcurrentMap(Function, Function, BinaryOperator)相反。
Function.identity()可能有帮助。例如,下面的生产
Map映射的学生平均成绩:
Map<Student, Double> studentToGPA
students.stream().collect(toMap(Functions.identity(),
student -> computeGPA(student)));
和以下产生一个唯一的标识符来学生
Map映射:
Map<String, Student> studentIdToStudent
students.stream().collect(toConcurrentMap(Student::getId,
Functions.identity());
这是一个concurrent和unordered收集器。
T -输入元素的类型
K -关键的映射函数的输出类型
U -值映射函数的输出类型
keyMapper的映射函数来产生密钥
valueMapper -映射函数产生的值
Collector收集元素为
ConcurrentMap的关键是应用一个键映射功能,输入元素的结果,其值将值映射函数的输入元素的结果
toMap(Function, Function),
toConcurrentMap(Function, Function, BinaryOperator),
toConcurrentMap(Function, Function, BinaryOperator, Supplier)
public static <T,K,U> Collector<T,?,ConcurrentMap<K,U>> toConcurrentMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction)
Collector积累成
ConcurrentMap元素的键和值是应用提供的函数映射到输入元素的结果。
如果映射键包含重复的(根据Object.equals(Object)),价值映射函数应用于每个元素,并对结果进行合并,使用提供的合并功能。
toConcurrentMap仅仅使用合并功能,把无条件的,但你可以写的更灵活的合并政策。例如,如果你有一股
Person,和你想制造一个“电话簿”的名称到地址的映射,但它是可能的,两个人都有相同的名字,你可以做如下优雅地处理这些冲突,并产生一个
Map名称映射到一个地址链接列表:
Map<String, String> phoneBook
people.stream().collect(toConcurrentMap(Person::getName,
Person::getAddress,
(s, a) -> s + ", " + a));
这是一个concurrent和unordered收集器。
T -输入元素的类型
K -关键的映射函数的输出类型
U -值映射函数的输出类型
keyMapper -映射函数来产生密钥
valueMapper -映射函数产生的值
mergeFunction -合并功能,用于解决具有相同的键相关联的值之间的碰撞,提供
Map.merge(Object, Object, BiFunction)
Collector收集元素为
ConcurrentMap的关键是应用一个键映射功能,输入元素的结果,其值将值映射函数的所有输入的元素等于核心并使用合并功能的结果
toConcurrentMap(Function, Function),
toConcurrentMap(Function, Function, BinaryOperator, Supplier),
toMap(Function, Function, BinaryOperator)
public static <T,K,U,M extends ConcurrentMap<K,U>> Collector<T,?,M> toConcurrentMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator<U> mergeFunction, Supplier<M> mapSupplier)
Collector积累成
ConcurrentMap元素的键和值是应用提供的函数映射到输入元素的结果。
如果映射键包含重复的(根据Object.equals(Object)),价值映射函数应用于每个元素,并对结果进行合并,使用提供的合并功能。通过提供的ConcurrentMap供应商创建功能。
这是一个concurrent和unordered收集器。
T -输入元素的类型
K -关键的映射函数的输出类型
U -值映射函数的输出类型
M -产生的
ConcurrentMap类型
keyMapper -映射函数来产生密钥
valueMapper -映射函数产生的值
mergeFunction -合并功能,用于解决具有相同的键相关联的值之间的碰撞,提供
Map.merge(Object, Object, BiFunction)
mapSupplier -一个函数返回一个新的、空的
Map,结果将被插入
Collector收集元素为
ConcurrentMap的关键是应用一个键映射功能,输入元素的结果,其值将值映射函数的所有输入的元素等于核心并使用合并功能的结果
toConcurrentMap(Function, Function),
toConcurrentMap(Function, Function, BinaryOperator),
toMap(Function, Function, BinaryOperator, Supplier)
public static <T> Collector<T,?,IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper)
Collector采用
int-producing映射功能,每个输入元素,并返回结果值汇总统计。
T -输入元素的类型
mapper -映射函数应用于每个元素
Collector
summarizingDouble(ToDoubleFunction),
summarizingLong(ToLongFunction)
public static <T> Collector<T,?,LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper)
Collector采用
long-producing映射功能,每个输入元素,并返回结果值汇总统计。
T -输入元素的类型
mapper的映射函数应用于每个元素
Collector
summarizingDouble(ToDoubleFunction),
summarizingInt(ToIntFunction)
public static <T> Collector<T,?,DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper)
Collector采用
double-producing映射功能,每个输入元素,并返回结果值汇总统计。
T -输入元素的类型
mapper -映射函数应用于每个元素
Collector
summarizingLong(ToLongFunction),
summarizingInt(ToIntFunction)
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.