10個(gè)實(shí)現(xiàn)Java集合,Map類型自由轉(zhuǎn)換的實(shí)用工具方法
整理了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微信公眾號(hào)開發(fā)第一步 公眾號(hào)接入和access_token管理
這篇文章主要為大家介紹了java微信公眾號(hào)開發(fā),主要內(nèi)容包括公眾號(hào)接入和access_token管理,感興趣的小伙伴們可以參考一下2016-01-01
Idea2023配置JavaWeb項(xiàng)目(最新)
本文將介紹如何配置JavaWeb項(xiàng)目,以在Idea中實(shí)現(xiàn)開發(fā)環(huán)境,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09
JAVA WSIMPORT生成WEBSERVICE客戶端401認(rèn)證過程圖解
這篇文章主要介紹了JAVA WSIMPORT生成WEBSERVICE客戶端401認(rèn)證過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
mybatis返回list<Integer>時(shí)resultType寫Integer問題
這篇文章主要介紹了mybatis返回list<Integer>時(shí)resultType寫Integer問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,2023-12-12
Java通過驅(qū)動(dòng)包(jar包)連接MySQL數(shù)據(jù)庫(kù)的步驟總結(jié)及驗(yàn)證方式
本文詳細(xì)介紹如何使用Java通過JDBC連接MySQL數(shù)據(jù)庫(kù),包括下載驅(qū)動(dòng)、配置Eclipse環(huán)境、檢測(cè)數(shù)據(jù)庫(kù)連接等關(guān)鍵步驟,感興趣的朋友一起看看吧2025-07-07
SpringBoot利用限速器RateLimiter實(shí)現(xiàn)單機(jī)限流的示例代碼
本文主要介紹了SpringBoot利用限速器RateLimiter實(shí)現(xiàn)單機(jī)限流的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
如何使用java agent修改字節(jié)碼并在springboot啟動(dòng)時(shí)自動(dòng)生效
本文介紹了JavaAgent的使用方法和在SpringBoot中的應(yīng)用,JavaAgent可以通過修改類的字節(jié)碼,實(shí)現(xiàn)對(duì)非Spring容器管理對(duì)象的AOP處理,演示了如何定義切面邏輯,實(shí)現(xiàn)接口mock,感興趣的朋友跟隨小編一起看看吧2024-10-10
MybatisPlus一對(duì)多聯(lián)表查詢及分頁(yè)的解決過程
文章主要介紹了在使用MyBatis-Plus進(jìn)行一對(duì)多查詢時(shí)遇到的分頁(yè)問題及解決方案,在一對(duì)多場(chǎng)景下,直接映射會(huì)導(dǎo)致分頁(yè)數(shù)據(jù)數(shù)量不正確,而使用子查詢映射則可以解決這個(gè)問題,此外,通過在Java代碼中使用Stream進(jìn)行關(guān)聯(lián)處理,可以進(jìn)一步優(yōu)化查詢性能,避免多次子查詢帶來的效率問題2025-12-12

