This commit is contained in:
Looly
2021-06-24 22:37:15 +08:00
parent 968d18db10
commit 069fd6827a
4 changed files with 52 additions and 5 deletions

View File

@@ -365,7 +365,11 @@ public class CollUtil {
* @return 单差集
*/
public static <T> Collection<T> subtract(Collection<T> coll1, Collection<T> coll2) {
final Collection<T> result = ObjectUtil.clone(coll1);
Collection<T> result = ObjectUtil.clone(coll1);
if(null == result){
result = CollUtil.create(coll1.getClass());
result.addAll(coll1);
}
result.removeAll(coll2);
return result;
}
@@ -995,6 +999,11 @@ public class CollUtil {
try {
list = (Collection<T>) ReflectUtil.newInstance(collectionType);
} catch (Exception e) {
// 无法创建当前类型的对象,尝试创建父类型对象
final Class<?> superclass = collectionType.getSuperclass();
if(null != superclass && collectionType != superclass){
return create(superclass);
}
throw new UtilException(e);
}
}
@@ -1141,6 +1150,10 @@ public class CollUtil {
}
Collection<T> collection2 = ObjectUtil.clone(collection);
if(null == collection2){
// 不支持clone
collection2 = create(collection.getClass());
}
if (isEmpty(collection2)) {
return collection2;
}

View File

@@ -332,7 +332,11 @@ public class ListUtil {
* @since 4.0.6
*/
public static <T> List<T> reverseNew(List<T> list) {
final List<T> list2 = ObjectUtil.clone(list);
List<T> list2 = ObjectUtil.clone(list);
if(null == list2){
// 不支持clone
list2 = new ArrayList<>(list);
}
return reverse(list2);
}

View File

@@ -629,6 +629,10 @@ public class MapUtil {
}
Map<K, V> map2 = ObjectUtil.clone(map);
if(null == map2){
// 不支持clone
map2 = new HashMap<>(map.size(), 1f);
}
if (isEmpty(map2)) {
return map2;
}
@@ -636,7 +640,7 @@ public class MapUtil {
map2.clear();
} catch (UnsupportedOperationException e) {
// 克隆后的对象不支持清空说明为不可变集合对象使用默认的ArrayList保存结果
map2 = new HashMap<>();
map2 = new HashMap<>(map.size(), 1f);
}
Entry<K, V> modified;
@@ -687,6 +691,10 @@ public class MapUtil {
return map;
}
Map<K, V> map2 = ObjectUtil.clone(map);
if(null == map2){
// 不支持clone
map2 = new HashMap<>(map.size(), 1f);
}
if (isEmpty(map2)) {
return map2;
}

View File

@@ -184,6 +184,28 @@ public class CollUtilTest {
Assert.assertEquals("x", subtract.iterator().next());
}
@Test
public void subtractSetTest() {
HashMap<String, Object> map1 = MapUtil.newHashMap();
HashMap<String, Object> map2 = MapUtil.newHashMap();
map1.put("1", "v1");
map1.put("2", "v2");
map2.put("2", "v2");
Collection<String> r2 = CollUtil.subtract(map1.keySet(), map2.keySet());
Assert.assertEquals("[1]", r2.toString());
}
@Test
public void subtractSetToListTest() {
HashMap<String, Object> map1 = MapUtil.newHashMap();
HashMap<String, Object> map2 = MapUtil.newHashMap();
map1.put("1", "v1");
map1.put("2", "v2");
map2.put("2", "v2");
List<String> r2 = CollUtil.subtractToList(map1.keySet(), map2.keySet());
Assert.assertEquals("[1]", r2.toString());
}
@Test
public void toMapListAndToListMapTest() {
HashMap<String, String> map1 = new HashMap<>();
@@ -709,7 +731,7 @@ public class CollUtilTest {
}
@Test
public void pageTest(){
public void pageTest() {
List<Dict> objects = CollUtil.newArrayList();
for (int i = 0; i < 10; i++) {
objects.add(Dict.create().set("name", "姓名:" + i));
@@ -719,7 +741,7 @@ public class CollUtilTest {
}
@Test
public void subtractToListTest(){
public void subtractToListTest() {
List<Long> list1 = Arrays.asList(1L, 2L, 3L);
List<Long> list2 = Arrays.asList(2L, 3L);