Class CollectionFactory

java.lang.Object
cloud.opencode.base.collections.CollectionFactory

public final class CollectionFactory extends Object
CollectionFactory - Factory for Creating Collections CollectionFactory - 创建集合的工厂

Provides factory methods for creating various collection types with consistent API across different implementations.

提供创建各种集合类型的工厂方法,跨不同实现提供一致的 API。

Features | 主要功能:

  • List factories - 列表工厂
  • Set factories - 集合工厂
  • Map factories - 映射工厂
  • Queue and Deque factories - 队列和双端队列工厂
  • Concurrent collection factories - 并发集合工厂

Usage Examples | 使用示例:

// Create lists - 创建列表
List<String> arrayList = CollectionFactory.newArrayList();
List<String> linkedList = CollectionFactory.newLinkedList();

// Create sets - 创建集合
Set<String> hashSet = CollectionFactory.newHashSet();
Set<String> linkedHashSet = CollectionFactory.newLinkedHashSet();
Set<String> treeSet = CollectionFactory.newTreeSet();

// Create maps - 创建映射
Map<String, Integer> hashMap = CollectionFactory.newHashMap();
Map<String, Integer> linkedHashMap = CollectionFactory.newLinkedHashMap();
Map<String, Integer> treeMap = CollectionFactory.newTreeMap();

// Create concurrent collections - 创建并发集合
Map<String, Integer> concurrentMap = CollectionFactory.newConcurrentHashMap();
Set<String> concurrentSet = CollectionFactory.newConcurrentHashSet();

Security | 安全性:

  • Thread-safe: Yes (stateless factory) - 线程安全: 是(无状态工厂)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-collections V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • newArrayList

      public static <E> ArrayList<E> newArrayList()
      Create an ArrayList. 创建 ArrayList。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new ArrayList | 新的 ArrayList
    • newArrayList

      @SafeVarargs public static <E> ArrayList<E> newArrayList(E... elements)
      Create an ArrayList with initial elements. 创建带初始元素的 ArrayList。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - initial elements | 初始元素
      Returns:
      new ArrayList | 新的 ArrayList
    • newArrayList

      public static <E> ArrayList<E> newArrayList(Iterable<? extends E> elements)
      Create an ArrayList from an iterable. 从可迭代对象创建 ArrayList。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - elements | 元素
      Returns:
      new ArrayList | 新的 ArrayList
    • newArrayListWithCapacity

      public static <E> ArrayList<E> newArrayListWithCapacity(int initialCapacity)
      Create an ArrayList with initial capacity. 创建指定容量的 ArrayList。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      initialCapacity - initial capacity | 初始容量
      Returns:
      new ArrayList | 新的 ArrayList
    • newLinkedList

      public static <E> LinkedList<E> newLinkedList()
      Create a LinkedList. 创建 LinkedList。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new LinkedList | 新的 LinkedList
    • newLinkedList

      public static <E> LinkedList<E> newLinkedList(Iterable<? extends E> elements)
      Create a LinkedList from an iterable. 从可迭代对象创建 LinkedList。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - elements | 元素
      Returns:
      new LinkedList | 新的 LinkedList
    • newCopyOnWriteArrayList

      public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList()
      Create a CopyOnWriteArrayList. 创建 CopyOnWriteArrayList。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new CopyOnWriteArrayList | 新的 CopyOnWriteArrayList
    • newHashSet

      public static <E> HashSet<E> newHashSet()
      Create a HashSet. 创建 HashSet。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new HashSet | 新的 HashSet
    • newHashSet

      @SafeVarargs public static <E> HashSet<E> newHashSet(E... elements)
      Create a HashSet with initial elements. 创建带初始元素的 HashSet。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - initial elements | 初始元素
      Returns:
      new HashSet | 新的 HashSet
    • newHashSet

      public static <E> HashSet<E> newHashSet(Iterable<? extends E> elements)
      Create a HashSet from an iterable. 从可迭代对象创建 HashSet。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - elements | 元素
      Returns:
      new HashSet | 新的 HashSet
    • newLinkedHashSet

      public static <E> LinkedHashSet<E> newLinkedHashSet()
      Create a LinkedHashSet. 创建 LinkedHashSet。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new LinkedHashSet | 新的 LinkedHashSet
    • newLinkedHashSet

      @SafeVarargs public static <E> LinkedHashSet<E> newLinkedHashSet(E... elements)
      Create a LinkedHashSet with initial elements. 创建带初始元素的 LinkedHashSet。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      elements - initial elements | 初始元素
      Returns:
      new LinkedHashSet | 新的 LinkedHashSet
    • newTreeSet

      public static <E extends Comparable<? super E>> TreeSet<E> newTreeSet()
      Create a TreeSet. 创建 TreeSet。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new TreeSet | 新的 TreeSet
    • newTreeSet

      public static <E> TreeSet<E> newTreeSet(Comparator<? super E> comparator)
      Create a TreeSet with a comparator. 创建带比较器的 TreeSet。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      comparator - the comparator | 比较器
      Returns:
      new TreeSet | 新的 TreeSet
    • newEnumSet

      @SafeVarargs public static <E extends Enum<E>> EnumSet<E> newEnumSet(E first, E... elements)
      Create an EnumSet from elements. 从元素创建 EnumSet。
      Type Parameters:
      E - enum type | 枚举类型
      Parameters:
      first - first element | 第一个元素
      elements - additional elements | 附加元素
      Returns:
      new EnumSet | 新的 EnumSet
    • newConcurrentHashSet

      public static <E> Set<E> newConcurrentHashSet()
      Create a concurrent hash set. 创建并发哈希集合。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new concurrent set | 新的并发集合
    • newCopyOnWriteArraySet

      public static <E> CopyOnWriteArraySet<E> newCopyOnWriteArraySet()
      Create a CopyOnWriteArraySet. 创建 CopyOnWriteArraySet。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new CopyOnWriteArraySet | 新的 CopyOnWriteArraySet
    • newHashMap

      public static <K,V> HashMap<K,V> newHashMap()
      Create a HashMap. 创建 HashMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new HashMap | 新的 HashMap
    • newHashMapWithCapacity

      public static <K,V> HashMap<K,V> newHashMapWithCapacity(int initialCapacity)
      Create a HashMap with initial capacity. 创建指定容量的 HashMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      initialCapacity - initial capacity | 初始容量
      Returns:
      new HashMap | 新的 HashMap
    • newLinkedHashMap

      public static <K,V> LinkedHashMap<K,V> newLinkedHashMap()
      Create a LinkedHashMap. 创建 LinkedHashMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new LinkedHashMap | 新的 LinkedHashMap
    • newTreeMap

      public static <K extends Comparable<? super K>, V> TreeMap<K,V> newTreeMap()
      Create a TreeMap. 创建 TreeMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new TreeMap | 新的 TreeMap
    • newTreeMap

      public static <K,V> TreeMap<K,V> newTreeMap(Comparator<? super K> comparator)
      Create a TreeMap with a comparator. 创建带比较器的 TreeMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      comparator - the comparator | 比较器
      Returns:
      new TreeMap | 新的 TreeMap
    • newEnumMap

      public static <K extends Enum<K>, V> EnumMap<K,V> newEnumMap(Class<K> keyType)
      Create an EnumMap. 创建 EnumMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      keyType - key enum type | 键枚举类型
      Returns:
      new EnumMap | 新的 EnumMap
    • newConcurrentHashMap

      public static <K,V> ConcurrentHashMap<K,V> newConcurrentHashMap()
      Create a ConcurrentHashMap. 创建 ConcurrentHashMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new ConcurrentHashMap | 新的 ConcurrentHashMap
    • newConcurrentSkipListMap

      public static <K extends Comparable<? super K>, V> ConcurrentSkipListMap<K,V> newConcurrentSkipListMap()
      Create a ConcurrentSkipListMap. 创建 ConcurrentSkipListMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new ConcurrentSkipListMap | 新的 ConcurrentSkipListMap
    • newIdentityHashMap

      public static <K,V> IdentityHashMap<K,V> newIdentityHashMap()
      Create an IdentityHashMap. 创建 IdentityHashMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new IdentityHashMap | 新的 IdentityHashMap
    • newWeakHashMap

      public static <K,V> WeakHashMap<K,V> newWeakHashMap()
      Create a WeakHashMap. 创建 WeakHashMap。
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new WeakHashMap | 新的 WeakHashMap
    • newArrayDeque

      public static <E> ArrayDeque<E> newArrayDeque()
      Create an ArrayDeque. 创建 ArrayDeque。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new ArrayDeque | 新的 ArrayDeque
    • newPriorityQueue

      public static <E extends Comparable<? super E>> PriorityQueue<E> newPriorityQueue()
      Create a PriorityQueue. 创建 PriorityQueue。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new PriorityQueue | 新的 PriorityQueue
    • newPriorityQueue

      public static <E> PriorityQueue<E> newPriorityQueue(Comparator<? super E> comparator)
      Create a PriorityQueue with a comparator. 创建带比较器的 PriorityQueue。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      comparator - the comparator | 比较器
      Returns:
      new PriorityQueue | 新的 PriorityQueue
    • newLinkedBlockingQueue

      public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue()
      Create a LinkedBlockingQueue. 创建 LinkedBlockingQueue。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new LinkedBlockingQueue | 新的 LinkedBlockingQueue
    • newArrayBlockingQueue

      public static <E> ArrayBlockingQueue<E> newArrayBlockingQueue(int capacity)
      Create an ArrayBlockingQueue. 创建 ArrayBlockingQueue。
      Type Parameters:
      E - element type | 元素类型
      Parameters:
      capacity - the capacity | 容量
      Returns:
      new ArrayBlockingQueue | 新的 ArrayBlockingQueue
    • newPriorityBlockingQueue

      public static <E extends Comparable<? super E>> PriorityBlockingQueue<E> newPriorityBlockingQueue()
      Create a PriorityBlockingQueue. 创建 PriorityBlockingQueue。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new PriorityBlockingQueue | 新的 PriorityBlockingQueue
    • newConcurrentLinkedQueue

      public static <E> ConcurrentLinkedQueue<E> newConcurrentLinkedQueue()
      Create a ConcurrentLinkedQueue. 创建 ConcurrentLinkedQueue。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new ConcurrentLinkedQueue | 新的 ConcurrentLinkedQueue
    • newConcurrentLinkedDeque

      public static <E> ConcurrentLinkedDeque<E> newConcurrentLinkedDeque()
      Create a ConcurrentLinkedDeque. 创建 ConcurrentLinkedDeque。
      Type Parameters:
      E - element type | 元素类型
      Returns:
      new ConcurrentLinkedDeque | 新的 ConcurrentLinkedDeque