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 a4334fd5c..ebd79895e 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 @@ -477,10 +477,8 @@ public class CollUtil { return isSorted ? new LinkedHashSet() : new HashSet(); } int initialCapacity = Math.max((int) (ts.length / .75f) + 1, 16); - HashSet set = isSorted ? new LinkedHashSet(initialCapacity) : new HashSet(initialCapacity); - for (T t : ts) { - set.add(t); - } + final HashSet set = isSorted ? new LinkedHashSet(initialCapacity) : new HashSet(initialCapacity); + Collections.addAll(set, ts); return set; } @@ -504,7 +502,7 @@ public class CollUtil { * @return HashSet对象 */ public static HashSet newHashSet(boolean isSorted, Collection collection) { - return isSorted ? new LinkedHashSet(collection) : new HashSet(collection); + return isSorted ? new LinkedHashSet<>(collection) : new HashSet<>(collection); } /** @@ -574,10 +572,8 @@ public class CollUtil { if (ArrayUtil.isEmpty(values)) { return list(isLinked); } - List arrayList = isLinked ? new LinkedList() : new ArrayList(values.length); - for (T t : values) { - arrayList.add(t); - } + final List arrayList = isLinked ? new LinkedList() : new ArrayList(values.length); + Collections.addAll(arrayList, values); return arrayList; } @@ -594,7 +590,7 @@ public class CollUtil { if (null == collection) { return list(isLinked); } - return isLinked ? new LinkedList(collection) : new ArrayList(collection); + return isLinked ? new LinkedList<>(collection) : new ArrayList<>(collection); } /** @@ -751,7 +747,7 @@ public class CollUtil { * @return {@link CopyOnWriteArrayList} */ public static CopyOnWriteArrayList newCopyOnWriteArrayList(Collection collection) { - return (null == collection) ? (new CopyOnWriteArrayList()) : (new CopyOnWriteArrayList(collection)); + return (null == collection) ? (new CopyOnWriteArrayList()) : (new CopyOnWriteArrayList<>(collection)); } /** @@ -783,7 +779,7 @@ public class CollUtil { */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static Collection create(Class collectionType) { - Collection list = null; + Collection list; if (collectionType.isAssignableFrom(AbstractCollection.class)) { // 抽象集合默认使用ArrayList list = new ArrayList<>(); @@ -944,7 +940,7 @@ public class CollUtil { return null; } - return sub(new ArrayList(list), start, end, step); + return sub(new ArrayList<>(list), start, end, step); } /** @@ -1623,10 +1619,8 @@ public class CollUtil { * @return treeSet */ public static TreeSet toTreeSet(Collection collection, Comparator comparator) { - final TreeSet treeSet = new TreeSet(comparator); - for (T t : collection) { - treeSet.add(t); - } + final TreeSet treeSet = new TreeSet<>(comparator); + treeSet.addAll(collection); return treeSet; } @@ -1640,7 +1634,7 @@ public class CollUtil { * @return {@link Enumeration} */ public static Enumeration asEnumeration(Iterator iter) { - return new IteratorEnumeration(iter); + return new IteratorEnumeration<>(iter); } /** @@ -1866,9 +1860,7 @@ public class CollUtil { */ public static Collection addAll(Collection collection, T[] values) { if (null != collection && null != values) { - for (T value : values) { - collection.add(value); - } + Collections.addAll(collection, values); } return collection; } @@ -1958,7 +1950,7 @@ public class CollUtil { result.add(list.get(index)); } } else { - Object[] array = ((Collection) collection).toArray(); + final Object[] array = collection.toArray(); for (int index : indexes) { if (index < 0) { index += size; @@ -2151,7 +2143,7 @@ public class CollUtil { * @return treeSet */ public static List sort(Collection collection, Comparator comparator) { - List list = new ArrayList(collection); + List list = new ArrayList<>(collection); Collections.sort(list, comparator); return list; } @@ -2229,7 +2221,7 @@ public class CollUtil { * @since 3.0.9 */ public static TreeMap sort(Map map, Comparator comparator) { - final TreeMap result = new TreeMap(comparator); + final TreeMap result = new TreeMap<>(comparator); result.putAll(map); return result; } @@ -2534,7 +2526,7 @@ public class CollUtil { * * @param 处理参数类型 */ - public static interface Consumer { + public interface Consumer { /** * 接受并处理一个参数 * @@ -2552,7 +2544,7 @@ public class CollUtil { * @param KEY类型 * @param VALUE类型 */ - public static interface KVConsumer { + public interface KVConsumer { /** * 接受并处理一对参数 * @@ -2571,7 +2563,7 @@ public class CollUtil { * @param 被计算hash的对象类型 * @since 3.2.2 */ - public static interface Hash { + public interface Hash { /** * 计算Hash值 *