mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix bug
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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<>();
|
||||
|
Reference in New Issue
Block a user