Ver código fonte

ObjectUtils对象新增获取集合第一个元素工具方法

Woody 1 mês atrás
pai
commit
928e439965

+ 12 - 0
framework-common/src/main/java/com/chelvc/framework/common/util/ObjectUtils.java

@@ -1661,6 +1661,18 @@ public final class ObjectUtils {
         return CodecUtils.md5(string);
     }
 
+    /**
+     * 获取集合第一个元素
+     *
+     * @param collection 对象集合
+     * @param <T>        元素类型
+     * @return 对象实例
+     */
+    public static <T> T first(Collection<T> collection) {
+        return isEmpty(collection) ? null : collection instanceof List ?
+                ((List<T>) collection).get(0) : collection.iterator().next();
+    }
+
     /**
      * 将枚举对象转换成对象列表
      *