mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
修改CollUtil.union等方法泛型,使其支持多种子类union一个父类集合
This commit is contained in:
parent
66f1d50704
commit
8c50bc829c
@ -297,7 +297,7 @@ public class CollUtil {
|
|||||||
* @return 并集的集合,返回 {@link ArrayList}
|
* @return 并集的集合,返回 {@link ArrayList}
|
||||||
*/
|
*/
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
public static <T> Collection<T> union(final Collection<T>... colls) {
|
public static <T> Collection<T> union(final Collection<? extends T>... colls) {
|
||||||
return CollectionOperation.of(colls).union();
|
return CollectionOperation.of(colls).union();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -312,7 +312,7 @@ public class CollUtil {
|
|||||||
* @return 并集的集合,返回 {@link LinkedHashSet}
|
* @return 并集的集合,返回 {@link LinkedHashSet}
|
||||||
*/
|
*/
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
public static <T> Set<T> unionDistinct(final Collection<T>... colls) {
|
public static <T> Set<T> unionDistinct(final Collection<? extends T>... colls) {
|
||||||
return CollectionOperation.of(colls).unionDistinct();
|
return CollectionOperation.of(colls).unionDistinct();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -327,7 +327,7 @@ public class CollUtil {
|
|||||||
* @return 并集的集合,返回 {@link ArrayList}
|
* @return 并集的集合,返回 {@link ArrayList}
|
||||||
*/
|
*/
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
public static <T> List<T> unionAll(final Collection<T>... colls) {
|
public static <T> List<T> unionAll(final Collection<? extends T>... colls) {
|
||||||
return CollectionOperation.of(colls).unionAll();
|
return CollectionOperation.of(colls).unionAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ public class CollectionOperation<E> {
|
|||||||
* @return CollectionOperation
|
* @return CollectionOperation
|
||||||
*/
|
*/
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
public static <E> CollectionOperation<E> of(final Collection<E>... colls) {
|
public static <E> CollectionOperation<E> of(final Collection<? extends E>... colls) {
|
||||||
return new CollectionOperation<>(colls);
|
return new CollectionOperation<>(colls);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,8 +56,9 @@ public class CollectionOperation<E> {
|
|||||||
*
|
*
|
||||||
* @param colls 集合数组
|
* @param colls 集合数组
|
||||||
*/
|
*/
|
||||||
public CollectionOperation(final Collection<E>[] colls) {
|
@SuppressWarnings("unchecked")
|
||||||
this.colls = colls;
|
public CollectionOperation(final Collection<? extends E>[] colls) {
|
||||||
|
this.colls = (Collection<E>[]) colls;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1110,6 +1110,26 @@ public class CollUtilTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ToString(callSuper = true)
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
static class Cat extends Animal {
|
||||||
|
|
||||||
|
public Cat(String name, Integer age) {
|
||||||
|
super(name, age);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ToString(callSuper = true)
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
static class Pig extends Animal {
|
||||||
|
|
||||||
|
public Pig(String name, Integer age) {
|
||||||
|
super(name, age);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getFirstTest() {
|
public void getFirstTest() {
|
||||||
Assertions.assertNull(CollUtil.getFirst(null));
|
Assertions.assertNull(CollUtil.getFirst(null));
|
||||||
@ -1181,4 +1201,27 @@ public class CollUtilTest {
|
|||||||
public void minNullTest() {
|
public void minNullTest() {
|
||||||
Assertions.assertNull(CollUtil.max(null));
|
Assertions.assertNull(CollUtil.max(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void unionExtendTest() {
|
||||||
|
List<Dog> dog = Arrays.asList(new Dog("dog1", 12), new Dog("dog2", 12));
|
||||||
|
List<Cat> cat = Arrays.asList(new Cat("cat1", 12), new Cat("cat2", 12));
|
||||||
|
Assertions.assertEquals(CollUtil.union(dog, cat).size(), dog.size() + cat.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void unionAllExtendTest() {
|
||||||
|
List<Dog> dog = Arrays.asList(new Dog("dog1", 12), new Dog("dog2", 12));
|
||||||
|
List<Cat> cat = Arrays.asList(new Cat("cat1", 12), new Cat("cat2", 12));
|
||||||
|
List<Pig> pig = Arrays.asList(new Pig("pig1", 12), new Pig("pig2", 12));
|
||||||
|
Assertions.assertEquals(CollUtil.unionAll(dog, cat, pig).size(), dog.size() + cat.size() + pig.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void unionDistinctExtendTest() {
|
||||||
|
List<Dog> dog = Arrays.asList(new Dog("dog1", 12), new Dog("dog1", 12)); // same
|
||||||
|
List<Cat> cat = Arrays.asList(new Cat("cat1", 12), new Cat("cat2", 12));
|
||||||
|
List<Pig> pig = Arrays.asList(new Pig("pig1", 12), new Pig("pig2", 12));
|
||||||
|
Assertions.assertEquals(CollUtil.unionDistinct(dog, cat, pig).size(), 5);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user