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

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

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

整理了10個(gè)方法,可以滿足 Collection、List、Set、Map 之間各種類(lèi)型轉(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 就是類(lèi)型 OrderItem
  • Collection<OrderItem>中提取 Key, Map 的 Value 根據(jù) OrderItem 類(lèi)型進(jìn)行轉(zhuǎn)化。
  • Map<Long, OrderItem> 中的value 轉(zhuǎn)化為 Map<Long, Double>
  • value 轉(zhuǎn)化時(shí),lamada表達(dá)式可以使用(v)->{}, 也可以使用 (k,v)->{ }。

集合類(lèi)型轉(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 集合類(lèi)型的子類(lèi),所以無(wú)需再轉(zhuǎn)化。

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

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

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

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

Collection 轉(zhuǎn)化為 Map

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

  • Collection<OrderItem>中提取 Key, Map 的 Value 就是類(lèi)型 OrderItem
  • Collection<OrderItem>中提取 Key, Map 的 Value 根據(jù) OrderItem 類(lèi)型進(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 就是類(lèi)型 OrderItem
  • Collection<OrderItem>中提取 Key, Map 的 Value 根據(jù) OrderItem 類(lèi)型進(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類(lèi)型自由轉(zhuǎn)換的實(shí)用工具方法的詳細(xì)內(nèi)容,更多關(guān)于Java類(lèi)型轉(zhuǎn)換的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

巢湖市| 汝城县| 齐齐哈尔市| 高唐县| 饶平县| 武冈市| 达州市| 东平县| 普兰店市| 台北县| 乃东县| 大名县| 阳新县| 鄂托克旗| 大同县| 东乡县| 甘德县| 武汉市| 厦门市| 冕宁县| 安陆市| 烟台市| 水城县| 屏东县| 阜新| 阳东县| 鄂托克前旗| 台东县| 张家川| 荆门市| 巢湖市| 张掖市| 武平县| 呼和浩特市| 晋州市| 托里县| 镇原县| 缙云县| 延津县| 化州市| 瑞丽市|