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 单差集
|
* @return 单差集
|
||||||
*/
|
*/
|
||||||
public static <T> Collection<T> subtract(Collection<T> coll1, Collection<T> coll2) {
|
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);
|
result.removeAll(coll2);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -995,6 +999,11 @@ public class CollUtil {
|
|||||||
try {
|
try {
|
||||||
list = (Collection<T>) ReflectUtil.newInstance(collectionType);
|
list = (Collection<T>) ReflectUtil.newInstance(collectionType);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
// 无法创建当前类型的对象,尝试创建父类型对象
|
||||||
|
final Class<?> superclass = collectionType.getSuperclass();
|
||||||
|
if(null != superclass && collectionType != superclass){
|
||||||
|
return create(superclass);
|
||||||
|
}
|
||||||
throw new UtilException(e);
|
throw new UtilException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1141,6 +1150,10 @@ public class CollUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Collection<T> collection2 = ObjectUtil.clone(collection);
|
Collection<T> collection2 = ObjectUtil.clone(collection);
|
||||||
|
if(null == collection2){
|
||||||
|
// 不支持clone
|
||||||
|
collection2 = create(collection.getClass());
|
||||||
|
}
|
||||||
if (isEmpty(collection2)) {
|
if (isEmpty(collection2)) {
|
||||||
return collection2;
|
return collection2;
|
||||||
}
|
}
|
||||||
|
@@ -332,7 +332,11 @@ public class ListUtil {
|
|||||||
* @since 4.0.6
|
* @since 4.0.6
|
||||||
*/
|
*/
|
||||||
public static <T> List<T> reverseNew(List<T> list) {
|
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);
|
return reverse(list2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -629,6 +629,10 @@ public class MapUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Map<K, V> map2 = ObjectUtil.clone(map);
|
Map<K, V> map2 = ObjectUtil.clone(map);
|
||||||
|
if(null == map2){
|
||||||
|
// 不支持clone
|
||||||
|
map2 = new HashMap<>(map.size(), 1f);
|
||||||
|
}
|
||||||
if (isEmpty(map2)) {
|
if (isEmpty(map2)) {
|
||||||
return map2;
|
return map2;
|
||||||
}
|
}
|
||||||
@@ -636,7 +640,7 @@ public class MapUtil {
|
|||||||
map2.clear();
|
map2.clear();
|
||||||
} catch (UnsupportedOperationException e) {
|
} catch (UnsupportedOperationException e) {
|
||||||
// 克隆后的对象不支持清空,说明为不可变集合对象,使用默认的ArrayList保存结果
|
// 克隆后的对象不支持清空,说明为不可变集合对象,使用默认的ArrayList保存结果
|
||||||
map2 = new HashMap<>();
|
map2 = new HashMap<>(map.size(), 1f);
|
||||||
}
|
}
|
||||||
|
|
||||||
Entry<K, V> modified;
|
Entry<K, V> modified;
|
||||||
@@ -687,6 +691,10 @@ public class MapUtil {
|
|||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
Map<K, V> map2 = ObjectUtil.clone(map);
|
Map<K, V> map2 = ObjectUtil.clone(map);
|
||||||
|
if(null == map2){
|
||||||
|
// 不支持clone
|
||||||
|
map2 = new HashMap<>(map.size(), 1f);
|
||||||
|
}
|
||||||
if (isEmpty(map2)) {
|
if (isEmpty(map2)) {
|
||||||
return map2;
|
return map2;
|
||||||
}
|
}
|
||||||
|
@@ -184,6 +184,28 @@ public class CollUtilTest {
|
|||||||
Assert.assertEquals("x", subtract.iterator().next());
|
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
|
@Test
|
||||||
public void toMapListAndToListMapTest() {
|
public void toMapListAndToListMapTest() {
|
||||||
HashMap<String, String> map1 = new HashMap<>();
|
HashMap<String, String> map1 = new HashMap<>();
|
||||||
@@ -709,7 +731,7 @@ public class CollUtilTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void pageTest(){
|
public void pageTest() {
|
||||||
List<Dict> objects = CollUtil.newArrayList();
|
List<Dict> objects = CollUtil.newArrayList();
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
objects.add(Dict.create().set("name", "姓名:" + i));
|
objects.add(Dict.create().set("name", "姓名:" + i));
|
||||||
@@ -719,7 +741,7 @@ public class CollUtilTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void subtractToListTest(){
|
public void subtractToListTest() {
|
||||||
List<Long> list1 = Arrays.asList(1L, 2L, 3L);
|
List<Long> list1 = Arrays.asList(1L, 2L, 3L);
|
||||||
List<Long> list2 = Arrays.asList(2L, 3L);
|
List<Long> list2 = Arrays.asList(2L, 3L);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user