mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
增加泛型集合向上、向下转型
This commit is contained in:
parent
3df525409b
commit
fb79f437ab
@ -3056,4 +3056,94 @@ public class CollUtil {
|
|||||||
|
|
||||||
return IterUtil.isEqualList(list1, list2);
|
return IterUtil.isEqualList(list1, list2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 泛型集合向上转型。例如将Collection<Integer>转换为Collection<Number>
|
||||||
|
* @param collection 集合
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return 泛化集合
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T> Collection<T> castup(Collection<? extends T> collection){
|
||||||
|
return (Collection<T>) collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 泛型集合向下转型。例如将Collection<Number>转换为Collection<Integer>
|
||||||
|
* @param collection
|
||||||
|
* @param <T>
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T> Collection<T> castdown(Collection<? super T> collection){
|
||||||
|
return (Collection<T>) collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 泛型集合向上转型。例如将Set<Integer>转换为Set<Number>
|
||||||
|
* @param set 集合
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return 泛化集合
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T> Set<T> castup(Set<? extends T> set){
|
||||||
|
return (Set<T>) set;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 泛型集合向下转型。例如将Set<Number>转换为Set<Integer>
|
||||||
|
* @param set 集合
|
||||||
|
* @param <T> 泛型子类
|
||||||
|
* @return 泛化集合
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T> Set<T> castdown(Set<? super T> set){
|
||||||
|
return (Set<T>) set;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 泛型集合向下转型。例如将Map<Integer, Integer>转换为Map<Number,Number>
|
||||||
|
* @param map 集合
|
||||||
|
* @param <K> 泛型父类
|
||||||
|
* @param <V> 泛型父类
|
||||||
|
* @return 泛化集合
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <K,V> Map<K,V> castup(Map<?extends K, ? extends V> map){
|
||||||
|
return (Map<K,V>) map;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 泛型集合向下转型。例如将Map<Number,Number>转换为Map<Integer, Integer>
|
||||||
|
* @param map 集合
|
||||||
|
* @param <K> 泛型子类
|
||||||
|
* @param <V> 泛型子类
|
||||||
|
* @return 泛化集合
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <K,V> Map<K,V> castdown(Map<?super K, ? super V> map){
|
||||||
|
return (Map<K,V>) map;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 泛型接口向上转型。例如将List<Integer>转换为List<Number>
|
||||||
|
* @param list 集合
|
||||||
|
* @param <T> 泛型的父类
|
||||||
|
* @return 泛化集合
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T> List<T> castup(List<? extends T> list){
|
||||||
|
return (List<T>) list;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 泛型集合向下转型。例如将List<Number>转换为List<Integer>
|
||||||
|
* @param list 集合
|
||||||
|
* @param <T> 泛型的子类
|
||||||
|
* @return 泛化集合
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T> List<T> castdown(List<? super T> list){
|
||||||
|
return (List<T>) list;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ import lombok.Data;
|
|||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@ -908,6 +909,36 @@ public class CollUtilTest {
|
|||||||
Assert.assertEquals("bb", distinct.get(1).getName());
|
Assert.assertEquals("bb", distinct.get(1).getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCastToSuper() {
|
||||||
|
Collection<Integer> collection=CollUtil.newLinkedList(1,2,3);
|
||||||
|
List<Integer> list = CollUtil.newArrayList(1, 2, 3);
|
||||||
|
Set<Integer> set = CollUtil.newHashSet(1, 2, 3);
|
||||||
|
Map<Integer, Integer> map = new HashMap<>();
|
||||||
|
map.put(1, 1);
|
||||||
|
|
||||||
|
Collection<Number> collection2 = CollUtil.castup(collection);
|
||||||
|
Assert.assertSame(collection, collection2);
|
||||||
|
|
||||||
|
Collection<Integer> collection3 = CollUtil.castdown(collection2);
|
||||||
|
Assert.assertSame(collection2, collection3);
|
||||||
|
|
||||||
|
List<Number> list2 = CollUtil.castup(list);
|
||||||
|
Assert.assertSame(list, list2);
|
||||||
|
List<Integer> list3 = CollUtil.castdown(list2);
|
||||||
|
Assert.assertSame(list2, list3);
|
||||||
|
|
||||||
|
Set<Number> set2 = CollUtil.castup(set);
|
||||||
|
Assert.assertSame(set, set2);
|
||||||
|
Set<Integer> set3 = CollUtil.castdown(set2);
|
||||||
|
Assert.assertSame(set2, set3);
|
||||||
|
|
||||||
|
Map<Number, Serializable> map2 = CollUtil.castup(map);
|
||||||
|
Assert.assertSame(map, map2);
|
||||||
|
Map<Integer, Number> map3 = CollUtil.castdown(map2);
|
||||||
|
Assert.assertSame(map2, map3);
|
||||||
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
static class Person {
|
static class Person {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user