youyichannel

志于道,据于德,依于仁,游于艺!

0%

Lambda 转换工具类

Base Env:

  • Hutool Core 5.8.25
  • JDK 8+

概览:

  • Collection<OrderItem> 转化为 List<OrderItem>
  • Collection<OrderItem> 转化为 Set<OrderItem>
  • List<OrderItem> 转化为 List<Long>
  • Set<OrderItem>转化为Set<Long>
  • Collection<OrderItem> 转化为 List<Long>
  • Collection<OrderItem> 转化为 Set<Long>
  • Collection<OrderItem>中提取 Key, MapValue 就是类型 OrderItem
  • Collection<OrderItem>中提取 Key, MapValue 根据 OrderItem 类型进行转化。
  • Map<Long, OrderItem> 中的 value 转化为 Map<Long, Double>
  • value 转化时,lambda 表达式可以使用(v)->{}, 也可以使用 (k,v)->{}
/**
* Utility class for converting between Collection (List, Set) and Map. Provides various conversion
* and mapping functionalities.
*
* @author <a href="https://github.com/yoyocraft">youyi</a>
*/
public class ConverterUtil {

/**
* Converts a Collection to a Map using the provided key mapping function.
*
* @param <T> the type of elements in the Collection
* @param <K> the type of keys for the Map
* @param data the Collection to be converted
* @param keyMapper the function to map elements to keys in the Map
* @return a Map containing the elements of the Collection with keys generated by the key
* mapping function
*/
public static <T, K> Map<K, T> toMap(Collection<T> data,
Function<? super T, ? extends K> keyMapper) {
return toMap(data, keyMapper, Function.identity());
}

private static <K, T> Map<K, T> toMap(Collection<T> data,
Function<? super T, ? extends K> keyMapper, Function<T, T> valueMapper) {
return toMap(data, keyMapper, valueMapper, pickSecond());
}

/**
* Converts a Collection to a Map using the provided key and value mapping functions, with an
* optional merge function for handling duplicate keys.
*
* @param <T> the type of elements in the Collection
* @param <K> the type of keys for the Map
* @param <V> the type of values for the Map
* @param data the Collection to be converted
* @param keyMapper the function to map elements to keys in the Map
* @param valueMapper the function to map elements to values in the Map
* @param mergerFunction the function to resolve collisions between values associated with the
* same key
* @return a Map containing the elements of the Collection with keys and values generated by the
* mapping functions
*/
public static <T, K, V> Map<K, V> toMap(Collection<T> data,
Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends V> valueMapper, BinaryOperator<V> mergerFunction) {
if (CollUtil.isEmpty(data)) {
return Collections.emptyMap();
}

return data.stream().collect(
Collectors.toMap(keyMapper, valueMapper, mergerFunction)
);
}

/**
* Converts the values of a Map using the provided function.
*
* @param <K> the type of keys for the input Map
* @param <V> the type of values for the input Map
* @param <C> the type of values for the output Map after conversion
* @param map the Map whose values are to be converted
* @param valueFunction the function to convert values in the Map
* @return a new Map with the same keys as the input Map but with values transformed by the
* provided function
*/
public static <K, V, C> Map<K, C> convertMapValue(Map<K, V> map,
BiFunction<K, V, C> valueFunction) {
return convertMapValue(map, valueFunction, pickSecond());
}

/**
* Converts the values of a Map using the provided function, with an optional merge function for
* handling duplicate keys.
*
* @param <K> the type of keys for the input Map
* @param <V> the type of values for the input Map
* @param <C> the type of values for the output Map after conversion
* @param map the Map whose values are to be converted
* @param valueFunction the function to convert values in the Map
* @param mergeFunction the function to resolve collisions between values after conversion
* @return a new Map with the same keys as the input Map but with values transformed by the
* provided function
*/
public static <K, V, C> Map<K, C> convertMapValue(Map<K, V> map,
BiFunction<K, V, C> valueFunction, BinaryOperator<C> mergeFunction) {
if (CollUtil.isEmpty(map)) {
return Collections.emptyMap();
}

return map.entrySet().stream().collect(
Collectors.toMap(
Map.Entry::getKey,
e -> valueFunction.apply(e.getKey(), e.getValue()),
mergeFunction
)
);
}

/**
* Returns a BinaryOperator that always selects the first operand.
*
* @param <T> the type of operands
* @return a BinaryOperator that picks the first operand
*/
public static <T> BinaryOperator<T> pickFirst() {
return (k1, k2) -> k1;
}

/**
* Returns a BinaryOperator that always selects the second operand.
*
* @param <T> the type of operands
* @return a BinaryOperator that picks the second operand
*/
public static <T> BinaryOperator<T> pickSecond() {
return (k1, k2) -> k2;
}

/**
* Converts a Collection to a List.
*
* @param <T> the type of elements in the Collection
* @param data the Collection to be converted
* @return a List containing the elements of the Collection
*/
public static <T> List<T> toList(Collection<T> data) {
if (CollUtil.isEmpty(data)) {
return Collections.emptyList();
}

if (data instanceof List<T>) {
return (List<T>) data;
}

return new ArrayList<>(data);
}

/**
* Converts a Collection to a Set.
*
* @param <T> the type of elements in the Collection
* @param data the Collection to be converted
* @return a Set containing the elements of the Collection
*/
public static <T> Set<T> toSet(Collection<T> data) {
if (CollUtil.isEmpty(data)) {
return Collections.emptySet();
}

if (data instanceof Set<T>) {
return (Set<T>) data;
}

return new HashSet<>(data);
}

/**
* Maps elements of a List to a new List using the provided mapping function.
*
* @param <T> the type of elements in the input List
* @param <R> the type of elements in the output List after mapping
* @param data the input List to be mapped
* @param mapper the function to apply to each element for mapping
* @return a new List containing the results of applying the mapping function to each element of
* the input List
*/
public static <T, R> List<R> map(List<T> data, Function<T, R> mapper) {
return data.stream().map(mapper).collect(Collectors.toList());
}

/**
* Maps elements of a Set to a new Set using the provided mapping function.
*
* @param <T> the type of elements in the input Set
* @param <R> the type of elements in the output Set after mapping
* @param data the input Set to be mapped
* @param mapper the function to apply to each element for mapping
* @return a new Set containing the results of applying the mapping function to each element of
* the input Set
*/
public static <T, R> Set<R> map(Set<T> data, Function<T, R> mapper) {
return data.stream().map(mapper).collect(Collectors.toSet());
}

/**
* Maps elements of a Collection to a new List using the provided mapping function.
*
* @param <T> the type of elements in the input Collection
* @param <R> the type of elements in the output List after mapping
* @param data the input Collection to be mapped
* @param mapper the function to apply to each element for mapping
* @return a new List containing the results of applying the mapping function to each element of
* the input Collection
*/
public static <T, R> List<R> mapToList(Collection<T> data, Function<T, R> mapper) {
return data.stream().map(mapper).collect(Collectors.toList());
}

/**
* Maps elements of a Collection to a new Set using the provided mapping function.
*
* @param <T> the type of elements in the input Collection
* @param <R> the type of elements in the output Set after mapping
* @param data the input Collection to be mapped
* @param mapper the function to apply to each element for mapping
* @return a new Set containing the results of applying the mapping function to each element of
* the input Collection
*/
public static <T, R> Set<R> mapToSet(Collection<T> data, Function<T, R> mapper) {
return data.stream().map(mapper).collect(Collectors.toSet());
}

}