Ten methods written by the technical director helped me become proficient in lambda expressions

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

  1. Convert Collection<OrderItem>toList<OrderItem>
  2. Convert Collection<OrderItem>toSet<OrderItem>
  3. Convert List<OrderItem>toList<Long>
  4. will be Set<OrderItem> converted to Set<Long>
  5. Convert Collection<OrderItem>toList<Long>
  6. Convert Collection<OrderItem>toSet<Long>
  7. Collection<OrderItem>Extract the Key from it
  8. Collection<OrderItem>The Key is extracted from it
  9. Convert Map<Long, OrderItem>the value in intoMap<Long, Double>
  10. When converting value, lamada expressions can be used (v)->{}and can also be used (k,v)->{ }.

See Collectionthe 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 Collectiontype conversion to Map type. There are two methods for converting Collection into Map.

  1. From Collection<OrderItem>to Map<Key, OrderItem>, extract the Key, and the Value of the Map is of type OrderItem
  2. From Collection<OrderItem>to Map<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>.

Java
Copy 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

Java
Copy code
public 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

  1. Convert the value in Map<Long, OrderItem> to Map<Long, Double>
  2. When converting value, the lamada expression can use (v)->{} or (k, v)->{}.

test sample

Java
Copy 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

Java
Copy code
public 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

  1. Convert Collection<OrderItem>toList<OrderItem>
  2. Convert Collection<OrderItem>toSet<OrderItem>
js
Copy code
public 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

Java
Copy 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?

Java
Copy code
public 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

  1. Convert List<OrderItem>toList<Long>
  2. will be Set<OrderItem> converted to Set<Long>
  3. Convert Collection<OrderItem>toList<Long>
  4. Convert Collection<OrderItem>toSet<Long>
Java
Copy 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

  1. Convert Collection<OrderItem>toList<OrderItem>
  2. Convert Collection<OrderItem>toSet<OrderItem>
  3. Convert List<OrderItem>toList<Long>
  4. will be Set<OrderItem> converted to Set<Long>
  5. Convert Collection<OrderItem>toList<Long>
  6. Convert Collection<OrderItem>toSet<Long>
  7. Collection<OrderItem>Extract the Key from it
  8. Collection<OrderItem>The Key is extracted from it
  9. Convert Map<Long, OrderItem>the value in intoMap<Long, Double>
  10. When converting value, lamada expressions can be used (v)->{}and can also be used (k,v)->{ }.
正文完