最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

10個(gè)實(shí)現(xiàn)Java集合,Map類型自由轉(zhuǎn)換的實(shí)用工具方法

 更新時(shí)間:2023年09月11日 10:33:35   作者:他是程序員  
這篇文章主要為大家整理了整理了10個(gè)實(shí)用工具方法,可以滿足?Collection、List、Set、Map?之間各種類型轉(zhuǎn)化,文中的示例代碼講解詳細(xì),需要的可以參考下

整理了10個(gè)方法,可以滿足 Collection、List、Set、Map 之間各種類型轉(zhuǎn)化。例如

  • Collection<OrderItem> 轉(zhuǎn)化為 List<OrderItem>
  • Collection<OrderItem> 轉(zhuǎn)化為 Set<OrderItem>
  • List<OrderItem> 轉(zhuǎn)化為 List<Long>
  • Set<OrderItem> 轉(zhuǎn)化為 Set<Long>
  • Collection<OrderItem> 轉(zhuǎn)化為 List<Long>
  • Collection<OrderItem> 轉(zhuǎn)化為 Set<Long>
  • Collection<OrderItem>中提取 Key, Map 的 Value 就是類型 OrderItem
  • Collection<OrderItem>中提取 Key, Map 的 Value 根據(jù) OrderItem 類型進(jìn)行轉(zhuǎn)化。
  • Map<Long, OrderItem> 中的value 轉(zhuǎn)化為 Map<Long, Double>
  • value 轉(zhuǎn)化時(shí),lamada表達(dá)式可以使用(v)->{}, 也可以使用 (k,v)->{ }。

集合類型轉(zhuǎn)化

Collection 和 List、Set 的轉(zhuǎn)化

  • Collection<OrderItem> 轉(zhuǎn)化為 List<OrderItem>
  • Collection<OrderItem> 轉(zhuǎn)化為 Set<OrderItem>
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());
}

測(cè)試樣例

@Test//將集合 Collection 轉(zhuǎn)化為 List
public void testToList() {
    Collection<OrderItem> collection = coll;
    List<OrderItem> list = toList(coll);
}
@Test//將集合 Collection 轉(zhuǎn)化為 Set
public void testToSet() {
    Collection<OrderItem> collection = coll;
    Set<OrderItem> set = toSet(collection);
}

List和 Set 是 Collection 集合類型的子類,所以無需再轉(zhuǎn)化。

List、Set 類型之間的轉(zhuǎn)換

業(yè)務(wù)中有時(shí)候需要將 List<A> 轉(zhuǎn)化為 List<B>。如何實(shí)現(xiàn)工具類呢?

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());
}

測(cè)試樣例

  • List<OrderItem> 轉(zhuǎn)化為 List<Long>
  • Set<OrderItem> 轉(zhuǎn)化為 Set<Long>
  • Collection<OrderItem> 轉(zhuǎn)化為 List<Long>
  • Collection<OrderItem> 轉(zhuǎn)化為 Set<Long>
@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());
}

接下來看 Collection 集合類型到 Map類型的轉(zhuǎn)化。

Collection 轉(zhuǎn)化為 Map

由于 List 和 Set 是 Collection 類型的子類,所以只需要實(shí)現(xiàn)Collection 類型轉(zhuǎn)化為 Map 類型即可。 Collection轉(zhuǎn)化為 Map 共分兩個(gè)方法

  • Collection<OrderItem>中提取 Key, Map 的 Value 就是類型 OrderItem
  • Collection<OrderItem>中提取 Key, Map 的 Value 根據(jù) OrderItem 類型進(jìn)行轉(zhuǎn)化。
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, pickLast());
}
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));
}

使用樣例

@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);
}

代碼示例中把Set<OrderItem> 轉(zhuǎn)化為 Map<Long, OrderItem>Map<Long ,Double>

Map格式轉(zhuǎn)換

轉(zhuǎn)換 Map 的 Value

  • 將 Map<Long, OrderItem> 中的value 轉(zhuǎn)化為 Map<Long, Double>
  • value 轉(zhuǎn)化時(shí),lamada表達(dá)式可以使用(v)->{}, 也可以使用 (k,v)->{ }。
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> convertValue(Map<K, V> originMap, BiFunction<K, V, C> valueConverter) {
    return convertValue(originMap, valueConverter, Lambdas.pickLast());
}
public static <T> BinaryOperator<T> pickFirst() {
    return (k1, k2) -> k1;
}
public static <T> BinaryOperator<T> pickSecond() {
    return (k1, k2) -> k2;
}

測(cè)試樣例

@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());
}

總結(jié)

以上樣例包含了如下的映射場(chǎng)景

  • Collection<OrderItem> 轉(zhuǎn)化為 List<OrderItem>
  • Collection<OrderItem> 轉(zhuǎn)化為 Set<OrderItem>
  • List<OrderItem> 轉(zhuǎn)化為 List<Long>
  • Set<OrderItem> 轉(zhuǎn)化為 Set<Long>
  • Collection<OrderItem> 轉(zhuǎn)化為 List<Long>
  • Collection<OrderItem> 轉(zhuǎn)化為 Set<Long>
  • Collection<OrderItem>中提取 Key, Map 的 Value 就是類型 OrderItem
  • Collection<OrderItem>中提取 Key, Map 的 Value 根據(jù) OrderItem 類型進(jìn)行轉(zhuǎn)化。
  • Map<Long, OrderItem> 中的value 轉(zhuǎn)化為 Map<Long, Double>
  • value 轉(zhuǎn)化時(shí),lamada表達(dá)式可以使用(v)->{}, 也可以使用 (k,v)->{ }

以上就是10個(gè)實(shí)現(xiàn)Java集合,Map類型自由轉(zhuǎn)換的實(shí)用工具方法的詳細(xì)內(nèi)容,更多關(guān)于Java類型轉(zhuǎn)換的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

青川县| 四平市| 长治县| 灵石县| 通化市| 务川| 卓尼县| 乌兰浩特市| 绥宁县| 郯城县| 新泰市| 嘉善县| 澎湖县| 太保市| 绥阳县| 河南省| 大同县| 龙州县| 当阳市| 涟水县| 崇文区| 老河口市| 七台河市| 大宁县| 炉霍县| 霍州市| 宜都市| 克拉玛依市| 浪卡子县| 博爱县| 泽库县| 新乡市| 夏河县| 呼伦贝尔市| 大姚县| 合肥市| 云龙县| 新河县| 武城县| 富平县| 澜沧|