10個(gè)實(shí)現(xiàn)Java集合,Map類(lèi)型自由轉(zhuǎn)換的實(shí)用工具方法
整理了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)文章
Java實(shí)現(xiàn)直接插入排序與折半插入排序的示例詳解
這篇文章主要為大家詳細(xì)介紹了插入排序中兩個(gè)常見(jiàn)的排序:直接插入排序與折半插入排序。本文用Java語(yǔ)言實(shí)現(xiàn)了這兩個(gè)排序算法,感興趣的可以學(xué)習(xí)一下2022-06-06
springboot starter自定義實(shí)現(xiàn)公共模塊方式
這篇文章主要介紹了springboot starter自定義實(shí)現(xiàn)公共模塊方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Java基本數(shù)據(jù)類(lèi)型和運(yùn)算符詳解
這篇文章主要介紹了Java基本數(shù)據(jù)類(lèi)型和運(yùn)算符,結(jié)合實(shí)例形式詳細(xì)分析了java基本數(shù)據(jù)類(lèi)型、數(shù)據(jù)類(lèi)型轉(zhuǎn)換、算術(shù)運(yùn)算符、邏輯運(yùn)算符等相關(guān)原理與操作技巧,需要的朋友可以參考下2020-02-02
SpringBoot Test 多線程報(bào)錯(cuò)的根本原因(dataSource already
在使用Springboot test進(jìn)行相關(guān)測(cè)試的時(shí)候,發(fā)現(xiàn)開(kāi)啟線程操作數(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ù)和攔截器詳解,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05
Java實(shí)現(xiàn)AES加密算法的簡(jiǎn)單示例分享
這篇文章主要介紹了Java實(shí)現(xiàn)AES加密算法的簡(jiǎn)單示例分享,AES算法是基于對(duì)密碼值的置換和替代,需要的朋友可以參考下2016-04-04

