曹耘豪的博客

Java之自定义Collector

  1. Collectors.Characteristics介绍
  2. 扩展工具类

Collectors.Characteristics介绍

标识每个Collector的特性,可以在执行collect时进行特定的优化

扩展工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
public class CollectorUtils {

/**
* 带初始化容量的ArrayList收集器。因为初始化了大小,所以不建议在并发场景使用
*
* @param initialCapacity ArrayList初始化容量
*/
public static <E> Collector<E, ?, List<E>> toArrayList(int initialCapacity) {
return toList(() -> new ArrayList<>(initialCapacity));
}

/**
* 基于LinkedList的List收集器
*/
public static <E> Collector<E, ?, List<E>> toLinkedList() {
return toList(LinkedList::new);
}

public static <E, L extends List<E>> Collector<E, ?, L> toList(Supplier<L> listFactory) {
return Collector.of(listFactory, List::add, (left, right) -> {
left.addAll(right);
return left;
});
}

/**
* 带初始化容量的HashSet收集器。因为初始化了大小,所以不建议在并发场景使用
*/
public static <E> Collector<E, ?, Set<E>> toHashSet(int expectedSize) {
return toSet(() -> Sets.newHashSetWithExpectedSize(expectedSize));
}

public static <E, S extends Set<E>> Collector<E, ?, S> toSet(Supplier<S> setFactory) {
return Collector.of(setFactory, Set::add, (left, right) -> {
if (left.size() < right.size()) {
right.addAll(left);
return right;
} else {
left.addAll(right);
return left;
}
}, UNORDERED);
}

/**
* 带初始化容量的HashMap收集器。因为初始化了大小,所以不建议在并发场景使用。使用replaceValueMerger策略
*/
public static <E, K, V> Collector<E, ?, Map<K, V>> toHashMap(int expectedSize,
Function<? super E, ? extends K> keyMapper,
Function<? super E, ? extends V> valueMapper) {
return toMap(() -> Maps.newHashMapWithExpectedSize(expectedSize), keyMapper, valueMapper);
}

public static <E, K, V, M extends Map<K, V>> Collector<E, ?, M> toMap(Supplier<M> mapFactory,
Function<? super E, ? extends K> keyMapper,
Function<? super E, ? extends V> valueMapper) {
return Collector.of(mapFactory, (map, element) -> {
map.put(keyMapper.apply(element), valueMapper.apply(element));
}, replaceValueMerger());
}

/**
* 带初始化容量的HashMap收集器。因为初始化了大小,所以不建议在并发场景使用
*
* @param expectedSize HashMap的ExpectedSize
* @param mergeFunction 合并方法,(oldValue, newValue) -> newValue
*/
public static <E, K, V> Collector<E, ?, Map<K, V>> toHashMap(int expectedSize,
Function<? super E, ? extends K> keyMapper,
Function<? super E, ? extends V> valueMapper,
BinaryOperator<V> mergeFunction) {
return Collector.of(() -> Maps.newHashMapWithExpectedSize(expectedSize), (map, element) -> {
map.merge(keyMapper.apply(element), valueMapper.apply(element), mergeFunction);
}, mapMerger(mergeFunction), UNORDERED);
}

/**
* 带初始化容量的HashMap收集器。因为初始化了大小,所以不建议在并发场景使用
*
* @param expectedSize HashMap的ExpectedSize
* @param mergeFunction3 优化的合并方法,合并时带key,(key, oldValue, newValue) -> newValue
*/
public static <E, K, V> Collector<E, ?, Map<K, V>> toHashMap(int expectedSize,
Function<E, K> keyMapper,
Function<E, V> valueMapper,
Function3<K, V, V, V> mergeFunction3) {
return Collector.of(() -> Maps.newHashMapWithExpectedSize(expectedSize), (map, item) -> {
K key = keyMapper.apply(item);
map.merge(key, valueMapper.apply(item), mapValueMerger(key, mergeFunction3));
}, mapMerger(mergeFunction3), UNORDERED);
}

private static <K, V, M extends Map<K, V>> BinaryOperator<M> replaceValueMerger() {
return (m1, m2) -> {
m1.putAll(m2);
return m1;
};
}

private static <K, V> BinaryOperator<V> mapValueMerger(K key, Function3<K, V, V, V> mergeFunction) {
return (v1, v2) -> mergeFunction.apply(key, v1, v2);
}

private static <K, V, M extends Map<K, V>> BinaryOperator<M> mapMerger(BinaryOperator<V> mergeFunction) {
return (m1, m2) -> {
for (Map.Entry<K, V> e : m2.entrySet())
m1.merge(e.getKey(), e.getValue(), mergeFunction);
return m1;
};
}

private static <K, V, M extends Map<K, V>> BinaryOperator<M> mapMerger(Function3<K, V, V, V> mergeFunction) {
return (m1, m2) -> {
for (Map.Entry<K, V> e : m2.entrySet())
m1.merge(e.getKey(), e.getValue(), mapValueMerger(e.getKey(), mergeFunction));
return m1;
};
}

}
   /