The technical director of the former company wrote a tool class to re-encapsulate Java Stream. It is very enjoyable to use and is used by the whole company.
I wrote it down myself, changed the name, and shared it with everyone.
A total of 10 tool methods have been compiled to meet various types of conversions between Collection, List, Set, and Map. For example
- Convert
Collection<OrderItem>
toList<OrderItem>
- Convert
Collection<OrderItem>
toSet<OrderItem>
- Convert
List<OrderItem>
toList<Long>
- will be
Set<OrderItem>
converted toSet<Long>
- Convert
Collection<OrderItem>
toList<Long>
- Convert
Collection<OrderItem>
toSet<Long>
Collection<OrderItem>
Extract the Key from itCollection<OrderItem>
The Key is extracted from it- Convert
Map<Long, OrderItem>
the value in intoMap<Long, Double>
- When converting value, lamada expressions can be used
(v)->{}
and can also be used(k,v)->{ }
.
See Collection
the conversion of collection type to Map type.
Collection converted to Map
Since List and Set are subclasses of Collection type, they only need to implement Collection
type conversion to Map type. There are two methods for converting Collection into Map.
- From
Collection<OrderItem>
toMap<Key, OrderItem>
, extract the Key, and the Value of the Map is of type OrderItem - From
Collection<OrderItem>
toMap<Key,Value>
, extract the Key, and the Value of the Map is converted according to the OrderItem type.
Usage examples
In the code example, Set<OrderItem>
convert to Map<Long, OrderItem>
and Map<Long ,Double>
.
JavaCopy code@Test
public void testToMap() {
Collection<OrderItem> collection = coll;
Set<OrderItem> set = toSet(collection);
Map<Long, OrderItem> map = toMap(set, OrderItem::getOrderId);
}
@Test
public void testToMapV2() {
Collection<OrderItem> collection = coll;
Set<OrderItem> set = toSet(collection);
Map<Long, Double> map = toMap(set, OrderItem::getOrderId, OrderItem::getActPrice);
}
Code display
JavaCopy codepublic static <T, K> Map<K, T> toMap(Collection<T> collection, Function<? super T, ? extends K> keyMapper) {
return toMap(collection, keyMapper, Function.identity());
}
public static <T, K, V> Map<K, V> toMap(Collection<T> collection,
Function<? super T, ? extends K> keyFunction,
Function<? super T, ? extends V> valueFunction) {
return toMap(collection, keyFunction, valueFunction, pickSecond());
}
public static <T, K, V> Map<K, V> toMap(Collection<T> collection,
Function<? super T, ? extends K> keyFunction,
Function<? super T, ? extends V> valueFunction,
BinaryOperator<V> mergeFunction) {
if (CollectionUtils.isEmpty(collection)) {
return new HashMap<>(0);
}
return collection.stream().collect(Collectors.toMap(keyFunction, valueFunction, mergeFunction));
}
public static <T> BinaryOperator<T> pickFirst() {
return (k1, k2) -> k1;
}
public static <T> BinaryOperator<T> pickSecond() {
return (k1, k2) -> k2;
}
Map format conversion
Convert the Map’s Value
- Convert the value in Map<Long, OrderItem> to Map<Long, Double>
- When converting value, the lamada expression can use (v)->{} or (k, v)->{}.
test sample
JavaCopy code@Test
public void testConvertValue() {
Collection<OrderItem> collection = coll;
Set<OrderItem> set = toSet(collection);
Map<Long, OrderItem> map = toMap(set, OrderItem::getOrderId);
Map<Long, Double> orderId2Price = convertMapValue(map, item -> item.getActPrice());
Map<Long, String> orderId2Token = convertMapValue(map, (id, item) -> id + item.getName());
}
Code display
JavaCopy codepublic static <K, V, C> Map<K, C> convertMapValue(Map<K, V> map,
BiFunction<K, V, C> valueFunction,
BinaryOperator<C> mergeFunction) {
if (isEmpty(map)) {
return new HashMap<>();
}
return map.entrySet().stream().collect(Collectors.toMap(
e -> e.getKey(),
e -> valueFunction.apply(e.getKey(), e.getValue()),
mergeFunction
));
}
public static <K, V, C> Map<K, C> convertMapValue(Map<K, V> originMap, BiFunction<K, V, C> valueConverter) {
return convertMapValue(originMap, valueConverter, Lambdas.pickSecond());
}
public static <T> BinaryOperator<T> pickFirst() {
return (k1, k2) -> k1;
}
public static <T> BinaryOperator<T> pickSecond() {
return (k1, k2) -> k2;
}
Collection type conversion
Conversion of Collection to List and Set
- Convert
Collection<OrderItem>
toList<OrderItem>
- Convert
Collection<OrderItem>
toSet<OrderItem>
jsCopy codepublic static <T> List<T> toList(Collection<T> collection) {
if (collection == null) {
return new ArrayList<>();
}
if (collection instanceof List) {
return (List<T>) collection;
}
return collection.stream().collect(Collectors.toList());
}
public static <T> Set<T> toSet(Collection<T> collection) {
if (collection == null) {
return new HashSet<>();
}
if (collection instanceof Set) {
return (Set<T>) collection;
}
return collection.stream().collect(Collectors.toSet());
}
test sample
JavaCopy code@Test//将集合 Collection 转化为 List
public void testToList() {
Collection<OrderItem> collection = coll;
List<OrderItem> list = toList(coll);
}
@Test//将集合 Collection 转化为 Set
public void testToSet() {
Collection<OrderItem> collection = coll;
Set<OrderItem> set = toSet(collection);
}
List and Set are subclasses of the Collection collection type, so there is no need to convert them.
Conversion between List and Set types
Sometimes in business it is necessary to List<A>
convert into List<B>
. How to implement the tool class?
JavaCopy codepublic static <T, R> List<R> map(List<T> collection, Function<T, R> mapper) {
return collection.stream().map(mapper).collect(Collectors.toList());
}
public static <T, R> Set<R> map(Set<T> collection, Function<T, R> mapper) {
return collection.stream().map(mapper).collect(Collectors.toSet());
}
public static <T, R> List<R> mapToList(Collection<T> collection, Function<T, R> mapper) {
return collection.stream().map(mapper).collect(Collectors.toList());
}
public static <T, R> Set<R> mapToSet(Collection<T> collection, Function<T, R> mapper) {
return collection.stream().map(mapper).collect(Collectors.toSet());
}
test sample
- Convert
List<OrderItem>
toList<Long>
- will be
Set<OrderItem>
converted toSet<Long>
- Convert
Collection<OrderItem>
toList<Long>
- Convert
Collection<OrderItem>
toSet<Long>
JavaCopy code@Test
public void testMapToList() {
Collection<OrderItem> collection = coll;
List<OrderItem> list = toList(coll);
List<Long> orderIdList = map(list, (item) -> item.getOrderId());
}
@Test
public void testMapToSet() {
Collection<OrderItem> collection = coll;
Set<OrderItem> set = toSet(coll);
Set<Long> orderIdSet = map(set, (item) -> item.getOrderId());
}
@Test
public void testMapToList2() {
Collection<OrderItem> collection = coll;
List<Long> orderIdList = mapToList(collection, (item) -> item.getOrderId());
}
@Test
public void testMapToSetV2() {
Collection<OrderItem> collection = coll;
Set<Long> orderIdSet = mapToSet(collection, (item) -> item.getOrderId());
}
To summarize, the above examples include the following mapping scenarios
- Convert
Collection<OrderItem>
toList<OrderItem>
- Convert
Collection<OrderItem>
toSet<OrderItem>
- Convert
List<OrderItem>
toList<Long>
- will be
Set<OrderItem>
converted toSet<Long>
- Convert
Collection<OrderItem>
toList<Long>
- Convert
Collection<OrderItem>
toSet<Long>
Collection<OrderItem>
Extract the Key from itCollection<OrderItem>
The Key is extracted from it- Convert
Map<Long, OrderItem>
the value in intoMap<Long, Double>
- When converting value, lamada expressions can be used
(v)->{}
and can also be used(k,v)->{ }
.