add size method

This commit is contained in:
Looly
2020-11-12 16:03:31 +08:00
parent c40622d1cd
commit af118cd953
3 changed files with 31 additions and 32 deletions

View File

@@ -16,6 +16,7 @@
* 【extra 】 增加表达式引擎封装ExpressionUtilpr#1203@Github * 【extra 】 增加表达式引擎封装ExpressionUtilpr#1203@Github
* 【core 】 增加enum转数字支持issue#I24QZY@Gitee * 【core 】 增加enum转数字支持issue#I24QZY@Gitee
* 【core 】 NumberUtil.toBigDecimal空白符转换为0issue#I24MRP@Gitee * 【core 】 NumberUtil.toBigDecimal空白符转换为0issue#I24MRP@Gitee
* 【core 】 CollUtil和IterUtil增加size方法pr#208@Gitee
### Bug修复 ### Bug修复
* 【core 】 修复DateUtil.current使用System.nanoTime的问题issue#1198@Github * 【core 】 修复DateUtil.current使用System.nanoTime的问题issue#1198@Github

View File

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

View File

@@ -828,13 +828,17 @@ public class IterUtil {
* *
* @param iterable Iterable对象 * @param iterable Iterable对象
* @return Iterable对象的元素数量 * @return Iterable对象的元素数量
* @since 5.4.8 * @since 5.5.0
*/ */
public static int size(final Iterable<?> iterable) { public static int size(final Iterable<?> iterable) {
if(null == iterable){
return 0;
}
if (iterable instanceof Collection<?>) { if (iterable instanceof Collection<?>) {
return ((Collection<?>) iterable).size(); return ((Collection<?>) iterable).size();
} else { } else {
return size(iterable != null ? iterable.iterator() : empty()); return size(iterable.iterator());
} }
} }
@@ -843,7 +847,7 @@ public class IterUtil {
* *
* @param iterator Iterator对象 * @param iterator Iterator对象
* @return Iterator对象的元素数量 * @return Iterator对象的元素数量
* @since 5.4.8 * @since 5.5.0
*/ */
public static int size(final Iterator<?> iterator) { public static int size(final Iterator<?> iterator) {
int size = 0; int size = 0;