|
|
@@ -21,7 +21,6 @@ import cn.hutool.core.util.ReflectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.core.util.TypeUtil;
|
|
|
|
|
|
-import java.lang.reflect.Array;
|
|
|
import java.lang.reflect.Type;
|
|
|
import java.util.AbstractCollection;
|
|
|
import java.util.ArrayList;
|
|
|
@@ -1384,7 +1383,7 @@ public class CollUtil {
|
|
|
|
|
|
R value;
|
|
|
for (T t : collection) {
|
|
|
- if(null == t && ignoreNull){
|
|
|
+ if (null == t && ignoreNull) {
|
|
|
continue;
|
|
|
}
|
|
|
value = func.apply(t);
|
|
|
@@ -2932,36 +2931,33 @@ public class CollUtil {
|
|
|
}
|
|
|
// ---------------------------------------------------------------------------------------------- Interface end
|
|
|
|
|
|
- /**
|
|
|
- * 获取Collection或者iterator的大小
|
|
|
- * <p>
|
|
|
- * 此方法可以处理的对象类型如下
|
|
|
- * <ul>
|
|
|
- * <li>Collection - the collection size
|
|
|
- * <li>Map - the map size
|
|
|
- * <li>Array - the array size
|
|
|
- * <li>Iterator - the number of elements remaining in the iterator
|
|
|
- * <li>Enumeration - the number of elements remaining in the enumeration
|
|
|
- * </ul>
|
|
|
- *
|
|
|
- * @param object 可以为空的对象
|
|
|
- * @return 如果object为空则返回0
|
|
|
- * @throws IllegalArgumentException 参数object不是Collection或者iterator
|
|
|
- * @since 5.4.8
|
|
|
- */
|
|
|
- public static int size(final Object object) {
|
|
|
+ /**
|
|
|
+ * 获取Collection或者iterator的大小,此方法可以处理的对象类型如下:
|
|
|
+ * <ul>
|
|
|
+ * <li>Collection - the collection size
|
|
|
+ * <li>Map - the map size
|
|
|
+ * <li>Array - the array size
|
|
|
+ * <li>Iterator - the number of elements remaining in the iterator
|
|
|
+ * <li>Enumeration - the number of elements remaining in the enumeration
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * @param object 可以为空的对象
|
|
|
+ * @return 如果object为空则返回0
|
|
|
+ * @throws IllegalArgumentException 参数object不是Collection或者iterator
|
|
|
+ * @since 5.5.0
|
|
|
+ */
|
|
|
+ public static int size(final Object object) {
|
|
|
if (object == null) {
|
|
|
return 0;
|
|
|
}
|
|
|
+
|
|
|
int total = 0;
|
|
|
- if (object instanceof Map<?,?>) {
|
|
|
+ if (object instanceof Map<?, ?>) {
|
|
|
total = ((Map<?, ?>) object).size();
|
|
|
} else if (object instanceof Collection<?>) {
|
|
|
total = ((Collection<?>) object).size();
|
|
|
} else if (object instanceof Iterable<?>) {
|
|
|
total = IterUtil.size((Iterable<?>) object);
|
|
|
- } else if (object instanceof Object[]) {
|
|
|
- total = ((Object[]) object).length;
|
|
|
} else if (object instanceof Iterator<?>) {
|
|
|
total = IterUtil.size((Iterator<?>) object);
|
|
|
} else if (object instanceof Enumeration<?>) {
|
|
|
@@ -2970,13 +2966,11 @@ public class CollUtil {
|
|
|
total++;
|
|
|
it.nextElement();
|
|
|
}
|
|
|
+ } else if (ArrayUtil.isArray(object)) {
|
|
|
+ total = ArrayUtil.length(object);
|
|
|
} else {
|
|
|
- try {
|
|
|
- total = Array.getLength(object);
|
|
|
- } catch (IllegalArgumentException e) {
|
|
|
- throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
|
|
|
- }
|
|
|
+ throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
|
|
|
}
|
|
|
return total;
|
|
|
- }
|
|
|
+ }
|
|
|
}
|