Modifier and Type | Field and Description |
---|---|
static Set<Collector.Characteristics> |
CH_ID
说明已包含IDENTITY_FINISH特征 为 Characteristics.IDENTITY_FINISH 的缩写
|
static Set<Collector.Characteristics> |
CH_NOID
说明不包含IDENTITY_FINISH特征
|
Constructor and Description |
---|
CollectorUtil() |
Modifier and Type | Method and Description |
---|---|
static <K,V> Collector<Map.Entry<K,V>,?,Map<K,V>> |
entryToMap()
用于
Stream<Entry> 转 Map 的情况 |
static <T,A,R> Collector<T,?,R> |
filtering(Predicate<? super T> predicate,
Collector<? super T,A,R> downstream)
过滤
|
static <T,K> Collector<T,?,Map<K,List<T>>> |
groupingBy(Function<? super T,? extends K> classifier)
提供对null值友好的groupingBy操作的
Collector 实现 |
static <T,K,A,D> Collector<T,?,Map<K,D>> |
groupingBy(Function<? super T,? extends K> classifier,
Collector<? super T,A,D> downstream)
提供对null值友好的groupingBy操作的
Collector 实现 |
static <T,K,R> Collector<T,?,Map<K,List<R>>> |
groupingBy(Function<? super T,? extends K> classifier,
Function<? super T,? extends R> valueMapper)
提供对null值友好的groupingBy操作的
Collector 实现,
对集合分组,然后对分组后的值集合进行映射 |
static <T,K,R,C extends Collection<R>> |
groupingBy(Function<? super T,? extends K> classifier,
Function<? super T,? extends R> valueMapper,
Supplier<C> valueCollFactory)
提供对null值友好的groupingBy操作的
Collector 实现,
对集合分组,然后对分组后的值集合进行映射 |
static <T,K,R,C extends Collection<R>,M extends Map<K,C>> |
groupingBy(Function<? super T,? extends K> classifier,
Function<? super T,? extends R> valueMapper,
Supplier<C> valueCollFactory,
Supplier<M> mapFactory)
提供对null值友好的groupingBy操作的
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)
提供对null值友好的groupingBy操作的
Collector 实现,可指定map类型 |
static <T> Collector<T,?,String> |
joining(CharSequence delimiter)
提供任意对象的Join操作的
Collector 实现,对象默认调用toString方法 |
static <T> Collector<T,?,String> |
joining(CharSequence delimiter,
CharSequence prefix,
CharSequence suffix,
Function<T,? extends CharSequence> toStringFunc)
提供任意对象的Join操作的
Collector 实现 |
static <T> Collector<T,?,String> |
joining(CharSequence delimiter,
Function<T,? extends CharSequence> toStringFunc)
提供任意对象的Join操作的
Collector 实现 |
static <K,V,M extends Map<K,V>> |
mapMerger(BinaryOperator<V> mergeFunction)
用户合并map的BinaryOperator,传入合并前需要对value进行的操作
|
static <K,V> Collector<Map<K,V>,?,Map<K,List<V>>> |
reduceListMap()
聚合这种数据类型:
Collection<Map<K,V>> => Map<K,List<V>>
其中key相同的value,会累加到List中 |
static <K,V,R extends Map<K,List<V>>> |
reduceListMap(Supplier<R> mapSupplier)
聚合这种数据类型:
Collection<Map<K,V>> => Map<K,List<V>>
其中key相同的value,会累加到List中 |
static <T> Collector<T,?,EasyStream<T>> |
toEasyStream()
将流转为
EasyStream |
static <T,K> Collector<T,List<T>,EntryStream<K,T>> |
toEntryStream(Function<? super T,? extends K> keyMapper)
将流转为
EntryStream |
static <T,K,V> Collector<T,List<T>,EntryStream<K,V>> |
toEntryStream(Function<? super T,? extends K> keyMapper,
Function<? super T,? extends V> valueMapper)
将流转为
EntryStream |
static <T,K> Collector<T,?,Map<K,T>> |
toMap(Function<? super T,? extends K> keyMapper)
对null友好的 toMap 操作的
Collector 实现,默认使用HashMap |
static <T,K,U> Collector<T,?,Map<K,U>> |
toMap(Function<? super T,? extends K> keyMapper,
Function<? super T,? extends U> valueMapper)
对null友好的 toMap 操作的
Collector 实现,默认使用HashMap |
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)
对null友好的 toMap 操作的
Collector 实现,默认使用HashMap |
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)
对null友好的 toMap 操作的
Collector 实现 |
static <T,R> Collector<T,List<T>,R> |
transform(Function<List<T>,R> mapper)
收集元素,将其转为
ArrayList 集合后,再对该集合进行转换,并最终返回转换后的结果。 |
static <T,R,C extends Collection<T>> |
transform(Supplier<C> collFactory,
Function<C,R> mapper)
收集元素,将其转为指定
Collection 集合后,再对该集合进行转换,并最终返回转换后的结果。 |
public static final Set<Collector.Characteristics> CH_ID
public static final Set<Collector.Characteristics> CH_NOID
public static <T> Collector<T,?,String> joining(CharSequence delimiter)
Collector
实现,对象默认调用toString方法T
- 对象类型delimiter
- 分隔符Collector
public static <T> Collector<T,?,String> joining(CharSequence delimiter, Function<T,? extends CharSequence> toStringFunc)
Collector
实现T
- 对象类型delimiter
- 分隔符toStringFunc
- 自定义指定对象转换为字符串的方法Collector
public static <T> Collector<T,?,String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix, Function<T,? extends CharSequence> toStringFunc)
Collector
实现T
- 对象类型delimiter
- 分隔符prefix
- 前缀suffix
- 后缀toStringFunc
- 自定义指定对象转换为字符串的方法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
实现,可指定map类型T
- 实体类型K
- 实体中的分组依据对应类型,也是Map中key的类型D
- 下游操作对应返回类型,也是Map中value的类型A
- 下游操作在进行中间操作时对应类型M
- 最后返回结果Map类型classifier
- 分组依据mapFactory
- 提供的mapdownstream
- 下游操作Collector
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
- 实体类型K
- 实体中的分组依据对应类型,也是Map中key的类型D
- 下游操作对应返回类型,也是Map中value的类型A
- 下游操作在进行中间操作时对应类型classifier
- 分组依据downstream
- 下游操作Collector
public static <T,K> Collector<T,?,Map<K,List<T>>> groupingBy(Function<? super T,? extends K> classifier)
Collector
实现T
- 实体类型K
- 实体中的分组依据对应类型,也是Map中key的类型classifier
- 分组依据Collector
public static <T,K,R,C extends Collection<R>,M extends Map<K,C>> Collector<T,?,M> groupingBy(Function<? super T,? extends K> classifier, Function<? super T,? extends R> valueMapper, Supplier<C> valueCollFactory, Supplier<M> mapFactory)
Collector
实现,
对集合分组,然后对分组后的值集合进行映射T
- 元素类型K
- 键类型R
- 值类型C
- 值集合类型M
- 返回的Map集合类型classifier
- 分组依据valueMapper
- 值映射方法valueCollFactory
- 值集合的工厂方法mapFactory
- Map集合的工厂方法Collector
public static <T,K,R,C extends Collection<R>> Collector<T,?,Map<K,C>> groupingBy(Function<? super T,? extends K> classifier, Function<? super T,? extends R> valueMapper, Supplier<C> valueCollFactory)
Collector
实现,
对集合分组,然后对分组后的值集合进行映射T
- 元素类型K
- 键类型R
- 值类型C
- 值集合类型classifier
- 分组依据valueMapper
- 值映射方法valueCollFactory
- 值集合的工厂方法Collector
public static <T,K,R> Collector<T,?,Map<K,List<R>>> groupingBy(Function<? super T,? extends K> classifier, Function<? super T,? extends R> valueMapper)
Collector
实现,
对集合分组,然后对分组后的值集合进行映射T
- 元素类型K
- 键类型R
- 值类型classifier
- 分组依据valueMapper
- 值映射方法Collector
public static <T,K,U> Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
Collector
实现,默认使用HashMapT
- 实体类型K
- map中key的类型U
- map中value的类型keyMapper
- 指定map中的keyvalueMapper
- 指定map中的valueCollector
实现public static <T,K> Collector<T,?,Map<K,T>> toMap(Function<? super T,? extends K> keyMapper)
Collector
实现,默认使用HashMapT
- 实体类型K
- map中key的类型keyMapper
- 指定map中的keyCollector
实现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
实现,默认使用HashMapT
- 实体类型K
- map中key的类型U
- map中value的类型keyMapper
- 指定map中的keyvalueMapper
- 指定map中的valuemergeFunction
- 合并前对value进行的操作Collector
实现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
实现T
- 实体类型K
- map中key的类型U
- map中value的类型M
- map的类型keyMapper
- 指定map中的keyvalueMapper
- 指定map中的valuemergeFunction
- 合并前对value进行的操作mapSupplier
- 最终需要的map类型Collector
实现public static <K,V,M extends Map<K,V>> BinaryOperator<M> mapMerger(BinaryOperator<V> mergeFunction)
K
- key的类型V
- value的类型M
- mapmergeFunction
- 合并前需要对value进行的操作public static <K,V> Collector<Map<K,V>,?,Map<K,List<V>>> reduceListMap()
Collection<Map<K,V>> => Map<K,List<V>>
其中key相同的value,会累加到List中K
- key的类型V
- value的类型public static <K,V,R extends Map<K,List<V>>> Collector<Map<K,V>,?,R> reduceListMap(Supplier<R> mapSupplier)
Collection<Map<K,V>> => Map<K,List<V>>
其中key相同的value,会累加到List中K
- key的类型V
- value的类型R
- 返回值的类型mapSupplier
- 可自定义map的类型如concurrentHashMap等public static <T,K> Collector<T,List<T>,EntryStream<K,T>> toEntryStream(Function<? super T,? extends K> keyMapper)
EntryStream
T
- 输入元素类型K
- 元素的键类型keyMapper
- 键的映射方法public static <T,K,V> Collector<T,List<T>,EntryStream<K,V>> toEntryStream(Function<? super T,? extends K> keyMapper, Function<? super T,? extends V> valueMapper)
EntryStream
T
- 输入元素类型K
- 元素的键类型V
- 元素的值类型keyMapper
- 键的映射方法valueMapper
- 值的映射方法public static <T> Collector<T,?,EasyStream<T>> toEasyStream()
EasyStream
T
- 输入元素类型public static <T,R,C extends Collection<T>> Collector<T,C,R> transform(Supplier<C> collFactory, Function<C,R> mapper)
Collection
集合后,再对该集合进行转换,并最终返回转换后的结果。
返回的收集器的效果等同于:
Collection<T> coll = Stream.of(a, b, c, d)
.collect(Collectors.toColl(collFactory));
R result = mapper.apply(coll);
R
- 返回值类型T
- 输入元素类型C
- 中间收集输入元素的集合类型collFactory
- 中间收集输入元素的集合的创建方法mapper
- 最终将元素集合映射为返回值的方法public static <T,R> Collector<T,List<T>,R> transform(Function<List<T>,R> mapper)
ArrayList
集合后,再对该集合进行转换,并最终返回转换后的结果。
返回的收集器的效果等同于:
List<T> coll = Stream.of(a, b, c, d)
.collect(Collectors.toList());
R result = mapper.apply(coll);
R
- 返回值类型T
- 输入元素类型mapper
- 最终将元素集合映射为返回值的方法public static <K,V> Collector<Map.Entry<K,V>,?,Map<K,V>> entryToMap()
Stream<Entry>
转 Map 的情况K
- key类型V
- value类型Copyright © 2025. All rights reserved.