欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • Java面向?qū)ο笾鄳B(tài)

    Java面向?qū)ο笾鄳B(tài)

    這篇文章主要介紹了Java面向?qū)ο笾鄳B(tài),文章以什么是多態(tài)、多態(tài)的實(shí)現(xiàn)條件、多態(tài)的訪問特點(diǎn)、多態(tài)的優(yōu)點(diǎn)和缺點(diǎn)的相關(guān)資料展開文章內(nèi)容,需要的小伙伴可以參考一下
    2021-10-10
  • Java實(shí)現(xiàn)直接插入排序與折半插入排序的示例詳解

    Java實(shí)現(xiàn)直接插入排序與折半插入排序的示例詳解

    這篇文章主要為大家詳細(xì)介紹了插入排序中兩個(gè)常見的排序:直接插入排序與折半插入排序。本文用Java語(yǔ)言實(shí)現(xiàn)了這兩個(gè)排序算法,感興趣的可以學(xué)習(xí)一下
    2022-06-06
  • springboot starter自定義實(shí)現(xiàn)公共模塊方式

    springboot starter自定義實(shí)現(xiàn)公共模塊方式

    這篇文章主要介紹了springboot starter自定義實(shí)現(xiàn)公共模塊方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • java中Websocket的使用方法例子

    java中Websocket的使用方法例子

    這篇文章主要給大家介紹了關(guān)于java中Websocket的使用方法,WebSocket是HTML5開始提供的一種在瀏覽器和服務(wù)器間進(jìn)行全雙工通信的協(xié)議,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • Java基本數(shù)據(jù)類型和運(yùn)算符詳解

    Java基本數(shù)據(jù)類型和運(yùn)算符詳解

    這篇文章主要介紹了Java基本數(shù)據(jù)類型和運(yùn)算符,結(jié)合實(shí)例形式詳細(xì)分析了java基本數(shù)據(jù)類型、數(shù)據(jù)類型轉(zhuǎn)換、算術(shù)運(yùn)算符、邏輯運(yùn)算符等相關(guān)原理與操作技巧,需要的朋友可以參考下
    2020-02-02
  • SpringBoot Test 多線程報(bào)錯(cuò)的根本原因(dataSource already closed)

    SpringBoot Test 多線程報(bào)錯(cuò)的根本原因(dataSource already

    在使用Springboot test進(jìn)行相關(guān)測(cè)試的時(shí)候,發(fā)現(xiàn)開啟線程操作數(shù)據(jù)庫(kù)的時(shí)候異常,這篇文章主要介紹了SpringBoot Test 多線程報(bào)錯(cuò):dataSource already closed的根本原因及解決方法,需要的朋友可以參考下
    2022-06-06
  • Struts2之Action接收請(qǐng)求參數(shù)和攔截器詳解

    Struts2之Action接收請(qǐng)求參數(shù)和攔截器詳解

    這篇文章主要介紹了Struts2之Action接收請(qǐng)求參數(shù)和攔截器詳解,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-05-05
  • Java文件(io)編程_文件字節(jié)流的使用方法

    Java文件(io)編程_文件字節(jié)流的使用方法

    下面小編就為大家?guī)硪黄狫ava文件(io)編程_文件字節(jié)流的使用方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • Java實(shí)現(xiàn)AES加密算法的簡(jiǎn)單示例分享

    Java實(shí)現(xiàn)AES加密算法的簡(jiǎn)單示例分享

    這篇文章主要介紹了Java實(shí)現(xiàn)AES加密算法的簡(jiǎn)單示例分享,AES算法是基于對(duì)密碼值的置換和替代,需要的朋友可以參考下
    2016-04-04
  • Spring中的@ResponseStatus使用

    Spring中的@ResponseStatus使用

    這篇文章主要介紹了Spring中的@ResponseStatus使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11

最新評(píng)論