diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java
index dffb2a987..99b80a977 100644
--- a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java
+++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java
@@ -21,6 +21,7 @@ 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;
@@ -2930,4 +2931,52 @@ public class CollUtil {
void accept(K key, V value, int index);
}
// ---------------------------------------------------------------------------------------------- Interface end
+
+ /**
+ * 获取Collection或者iterator的大小
+ *
+ * 此方法可以处理的对象类型如下
+ *
+ * - Collection - the collection size
+ *
- Map - the map size
+ *
- Array - the array size
+ *
- Iterator - the number of elements remaining in the iterator
+ *
- Enumeration - the number of elements remaining in the enumeration
+ *
+ *
+ * @param object 可以为空的对象
+ * @return 如果object为空则返回0
+ * @throws IllegalArgumentException 参数object不是Collection或者iterator
+ * @since 5.4.8
+ */
+ public static int size(final Object object) {
+ if (object == null) {
+ return 0;
+ }
+ int total = 0;
+ 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>) {
+ final Enumeration> it = (Enumeration>) object;
+ while (it.hasMoreElements()) {
+ total++;
+ it.nextElement();
+ }
+ } else {
+ try {
+ total = Array.getLength(object);
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
+ }
+ }
+ return total;
+ }
}
diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/IterUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/IterUtil.java
index 3a5881dd0..b54718e55 100644
--- a/hutool-core/src/main/java/cn/hutool/core/collection/IterUtil.java
+++ b/hutool-core/src/main/java/cn/hutool/core/collection/IterUtil.java
@@ -10,6 +10,7 @@ import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
@@ -821,4 +822,37 @@ public class IterUtil {
public static Iterator trans(Iterator iterator, Function super F, ? extends T> function) {
return new TransIter<>(iterator, function);
}
+
+ /**
+ * 返回 Iterable 对象的元素数量
+ *
+ * @param iterable Iterable对象
+ * @return Iterable对象的元素数量
+ * @since 5.4.8
+ */
+ public static int size(final Iterable> iterable) {
+ if (iterable instanceof Collection>) {
+ return ((Collection>) iterable).size();
+ } else {
+ return size(iterable != null ? iterable.iterator() : empty());
+ }
+ }
+
+ /**
+ * 返回 Iterator 对象的元素数量
+ *
+ * @param iterator Iterator对象
+ * @return Iterator对象的元素数量
+ * @since 5.4.8
+ */
+ public static int size(final Iterator> iterator) {
+ int size = 0;
+ if (iterator != null) {
+ while (iterator.hasNext()) {
+ iterator.next();
+ size++;
+ }
+ }
+ return size;
+ }
}