Class Collections

java.lang.Object
one.tranic.t.util.Collections

public class Collections extends Object
The Collections class provides utility methods for creating optimized data structures such as maps, sets, and lists.

It dynamically determines whether to use the optimized implementations from the FastUtil library or standard Java collections based on the availability of FastUtil classes.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <K, V> void
    entryForEach(Map<K,V> map, Consumer<? super Map.Entry<K,V>> consumer)
    Iterates over each entry in the provided map and applies the given consumer action to each entry.
    static <T> List<T>
    Creates a new empty list instance.
    static <T> List<T>
    newArrayList(@org.jetbrains.annotations.Range(from=0L, to=2147483647L) int size)
    Creates a new List instance with the specified initial size.
    static <T> List<T>
    newArrayList(@NotNull Collection<? extends T> elements)
    Creates a new list containing the elements from the specified collection.
    static <T> List<T>
    newArrayList(@NotNull T... elements)
    Creates a new array-backed List containing the provided elements.
    static <K> Map<K,Boolean>
    Creates and returns a new Map instance where the values are of type Boolean.
    static <K> Map<K,Boolean>
    newBooleanHashMap(@org.jetbrains.annotations.Range(from=0L, to=2147483647L) int initialCapacity)
    Creates a new Map instance with keys of type K and boolean values.
    static <K> Map<K,Double>
    Creates a new map with generic keys and double values.
    static <K> Map<K,Double>
    newDoubleHashMap(@org.jetbrains.annotations.Range(from=0L, to=2147483647L) int initialCapacity)
    Creates a new hash map with keys of type K and values of type Double.
    static <K> Map<K,Float>
    Creates a new map with keys of type K and values of type Float.
    static <K> Map<K,Float>
    newFloatHashMap(@org.jetbrains.annotations.Range(from=0L, to=2147483647L) int initialCapacity)
    Creates a new map with keys of generic type K and float values.
    static <K, V> Map<K,V>
    Creates and returns a new hash map with default settings.
    static <K, V> Map<K,V>
    newHashMap(@org.jetbrains.annotations.Range(from=0L, to=2147483647L) int initialCapacity)
    Creates a new hash map with the specified initial capacity.
    static <K, V> Map<K,V>
    newHashMap(@NotNull Map<K,V> map)
    Creates a new HashMap instance and populates it with the entries from the provided map.
    static <T> Set<T>
    Creates a new empty HashSet instance.
    static <T> Set<T>
    newHashSet(@org.jetbrains.annotations.Range(from=0L, to=2147483647L) int initialCapacity)
    Creates a new hash set with the specified initial capacity.
    static <K> Map<K,Integer>
    Creates a new hash map that maps keys of type K to integer values.
    static <K> Map<K,Integer>
    newIntHashMap(@org.jetbrains.annotations.Range(from=0L, to=2147483647L) int initialCapacity)
    Creates a new hash map where keys are of type K and values are of type Integer.
    static <K> Map<K,Long>
    Creates and returns a new map instance where the keys are of generic type K, and the values are of type Long.
    static <K> Map<K,Long>
    newLongHashMap(@org.jetbrains.annotations.Range(from=0L, to=2147483647L) int initialCapacity)
    Creates a new hash map with keys of type K and values of type Long.
    static <T> Set<T>
    newTreeSet(Collection<? extends T> c)
    Creates a new TreeSet with the elements provided in the specified collection.
    static <T> Set<T>
    newTreeSetRB(@NotNull Collection<? extends T> c)
    Creates a new TreeSet instance with the elements provided in the specified collection.
    static <T> Set<T>
    Creates a new unmodifiable empty set.
    static <T> Set<T>
    newUnmodifiableHashSet(@NotNull Set<T> set)
    Creates a new unmodifiable hash set containing the elements of the provided set.
    static <T> Set<T>
    newUnmodifiableHashSet(@NotNull T... elements)
    Creates a new unmodifiable Set that contains the specified elements.
    static <T> List<T>
    Creates a new unmodifiable list.
    static <T> List<T>
    newUnmodifiableList(@NotNull List<T> list)
    Creates a new unmodifiable list from the provided list.
    static <T> List<T>
    newUnmodifiableList(@NotNull T... elements)
    Creates a new unmodifiable list containing the specified elements.
    static <K, V> boolean
    removeIf(Map<K,V> map, Predicate<? super Map.Entry<K,V>> filter)
    Removes all entries from the specified map that satisfy the provided predicate.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Collections

      public Collections()
  • Method Details

    • newIntHashMap

      public static <K> Map<K,Integer> newIntHashMap()
      Creates a new hash map that maps keys of type K to integer values.

      Depending on the configuration, the implementation may use either Object2IntOpenHashMap from fastutil or HashMap from Java's standard library.

      Type Parameters:
      K - the type of keys maintained by this map
      Returns:
      a new map that maps keys of type K to integers
    • newIntHashMap

      public static <K> Map<K,Integer> newIntHashMap(@org.jetbrains.annotations.Range(from=0L, to=2147483647L) int initialCapacity)
      Creates a new hash map where keys are of type K and values are of type Integer.

      Depending on the underlying configuration, this method may create either a HashMap instance or a fastutil Object2IntOpenHashMap instance.

      Parameters:
      initialCapacity - the initial capacity of the hash map. Must be a non-negative integer.
      Returns:
      a new map instance with keys of type K and values of type Integer.
    • newLongHashMap

      public static <K> Map<K,Long> newLongHashMap()
      Creates and returns a new map instance where the keys are of generic type K, and the values are of type Long.

      The specific implementation of the map depends on the runtime evaluation of the `fastutil` flag.

      If `fastutil` is true, the map will be an instance of `it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap`.

      Otherwise, it will fall back to a standard Java `HashMap`.

      Type Parameters:
      K - the type of keys maintained by the map
      Returns:
      a newly created map with generic key type K and Long values
    • newLongHashMap

      public static <K> Map<K,Long> newLongHashMap(@org.jetbrains.annotations.Range(from=0L, to=2147483647L) int initialCapacity)
      Creates a new hash map with keys of type K and values of type Long.

      Optionally utilizes a specialized implementation for performance optimization if the fastutil flag is enabled.

      Type Parameters:
      K - the type of keys to be used in the map
      Parameters:
      initialCapacity - the initial capacity of the hash map; must be greater than or equal to 0
      Returns:
      a new hash map instance with the specified initial capacity, either using a specialized implementation or a standard HashMap
    • newFloatHashMap

      public static <K> Map<K,Float> newFloatHashMap()
      Creates a new map with keys of type K and values of type Float.

      This method returns an instance of either Object2FloatOpenHashMap or HashMap, based on the runtime environment configuration. It is designed to optimize performance when handling float values as map entries.

      Type Parameters:
      K - the type of keys in the map
      Returns:
      a new map capable of storing keys of type K and values of type Float
    • newFloatHashMap

      public static <K> Map<K,Float> newFloatHashMap(@org.jetbrains.annotations.Range(from=0L, to=2147483647L) int initialCapacity)
      Creates a new map with keys of generic type K and float values.

      The initial capacity of the map is specified by the provided parameter.

      This method switches between using a FastUtil implementation or a standard Java HashMap depending on the underlying configuration.

      Type Parameters:
      K - the type of keys maintained by the map
      Parameters:
      initialCapacity - the initial capacity of the map; must be non-negative
      Returns:
      a new map instance with the respective initial capacity
    • newDoubleHashMap

      public static <K> Map<K,Double> newDoubleHashMap()
      Creates a new map with generic keys and double values.

      If the `fastutil` flag is true, an instance of `Object2DoubleOpenHashMap` from the FastUtil library is returned. Otherwise, a standard Java `HashMap` is created.

      Type Parameters:
      K - the type of keys maintained by this map
      Returns:
      a new map with keys of type K and values of type Double
    • newDoubleHashMap

      public static <K> Map<K,Double> newDoubleHashMap(@org.jetbrains.annotations.Range(from=0L, to=2147483647L) int initialCapacity)
      Creates a new hash map with keys of type K and values of type Double.

      The map's initial capacity is specified by the parameter.

      Depending on the internal configuration, it either creates an instance of Object2DoubleOpenHashMap from the FastUtil library or a standard HashMap.

      Parameters:
      initialCapacity - the initial capacity of the map. Must be a non-negative integer.
      Returns:
      a new map of type Map<K, Double> with the specified initial capacity.
    • newBooleanHashMap

      public static <K> Map<K,Boolean> newBooleanHashMap()
      Creates and returns a new Map instance where the values are of type Boolean.

      The type of Map implementation used depends on the value of the `fastutil` field:

      - If `fastutil` is true, an instance of `it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap` is returned.

      - If `fastutil` is false, a standard `HashMap` is returned.

      Type Parameters:
      K - the type of the keys in the map
      Returns:
      a new Map instance with Boolean values
    • newBooleanHashMap

      public static <K> Map<K,Boolean> newBooleanHashMap(@org.jetbrains.annotations.Range(from=0L, to=2147483647L) int initialCapacity)
      Creates a new Map instance with keys of type K and boolean values.

      Allows specifying the initial capacity for the map to optimize performance.

      If the `fastutil` flag is enabled, it uses a `Object2BooleanOpenHashMap` from the fastutil library; otherwise, it defaults to using a standard HashMap.

      Parameters:
      initialCapacity - the initial capacity of the map; must be a non-negative integer.
      Returns:
      a new Map instance with the specified initial capacity.
    • newHashMap

      public static <K, V> Map<K,V> newHashMap()
      Creates and returns a new hash map with default settings.

      The implementation of the hash map may vary based on internal configuration.

      Type Parameters:
      K - the type of keys maintained by this map
      V - the type of mapped values
      Returns:
      a new instance of a hash map
    • newHashMap

      public static <K, V> Map<K,V> newHashMap(@org.jetbrains.annotations.Range(from=0L, to=2147483647L) int initialCapacity)
      Creates a new hash map with the specified initial capacity.
      Parameters:
      initialCapacity - the initial capacity of the hash map; must be a non-negative integer
      Returns:
      a new instance of a hash map with the given initial capacity
    • newHashMap

      public static <K, V> Map<K,V> newHashMap(@NotNull @NotNull Map<K,V> map)
      Creates a new HashMap instance and populates it with the entries from the provided map.

      Depending on the configuration, it will use either a standard HashMap or a fastutil-specific Object2ObjectOpenHashMap implementation.

      Parameters:
      map - the map whose entries are to be added to the newly created map; must not be null
      Returns:
      a new map containing all entries from the provided map
    • newHashSet

      public static <T> Set<T> newHashSet()
      Creates a new empty HashSet instance.

      Depending on the configuration, this method returns either an instance of it.unimi.dsi.fastutil.objects.ObjectOpenHashSet if the fastutil optimization is enabled, or a standard java.util.HashSet otherwise.

      Type Parameters:
      T - the type of elements maintained by the set.
      Returns:
      a new empty Set instance.
    • newUnmodifiableHashSet

      public static <T> Set<T> newUnmodifiableHashSet()
      Creates a new unmodifiable empty set. The implementation of the set may vary depending on the internal configuration.
      Type Parameters:
      T - the type of elements that the set can hold
      Returns:
      a new unmodifiable empty set
    • newUnmodifiableHashSet

      public static <T> Set<T> newUnmodifiableHashSet(@NotNull @NotNull Set<T> set)
      Creates a new unmodifiable hash set containing the elements of the provided set.
      Parameters:
      set - the input set whose elements will be included in the new unmodifiable set; must not be null
      Returns:
      an unmodifiable set containing the elements of the input set
    • newUnmodifiableHashSet

      @SafeVarargs public static <T> Set<T> newUnmodifiableHashSet(@NotNull @NotNull T... elements)
      Creates a new unmodifiable Set that contains the specified elements.
      Parameters:
      elements - the elements to be included in the set; cannot be null.
      Returns:
      an unmodifiable set containing the specified elements.
    • newHashSet

      public static <T> Set<T> newHashSet(@org.jetbrains.annotations.Range(from=0L, to=2147483647L) int initialCapacity)
      Creates a new hash set with the specified initial capacity.
      Parameters:
      initialCapacity - the initial capacity of the hash set; must be non-negative
      Returns:
      a newly created hash set instance with the specified initial capacity
    • newTreeSet

      public static <T> Set<T> newTreeSet(Collection<? extends T> c)
      Creates a new TreeSet with the elements provided in the specified collection. Depending on the context, either a TreeSet or a ObjectAVLTreeSet from the fastutil library will be created and returned.
      Type Parameters:
      T - the type of elements maintained by the set
      Parameters:
      c - the collection whose elements are to be placed into the new set
      Returns:
      a newly created TreeSet containing the elements from the provided collection
    • newTreeSetRB

      public static <T> Set<T> newTreeSetRB(@NotNull @NotNull Collection<? extends T> c)
      Creates a new TreeSet instance with the elements provided in the specified collection. Depending on the configuration, it uses either a fastutil ObjectRBTreeSet or a standard TreeSet.
      Type Parameters:
      T - the type of elements maintained by the set
      Parameters:
      c - the collection whose elements are to be placed into the new set; must not be null
      Returns:
      a newly created TreeSet containing all elements of the specified collection
    • newArrayList

      public static <T> List<T> newArrayList()
      Creates a new empty list instance. The implementation type of the list returned depends on the runtime configuration.
      Type Parameters:
      T - The type of elements that the list will hold.
      Returns:
      A new instance of an empty List.
    • newUnmodifiableList

      public static <T> List<T> newUnmodifiableList()
      Creates a new unmodifiable list. The implementation used for the unmodifiable list depends on whether the `fastutil` flag is enabled. If `fastutil` is true, it uses the `ObjectList.of()` from the FastUtil library. Otherwise, it uses `Collections.unmodifiableList` wrapping an empty `ArrayList`.
      Type Parameters:
      T - the type of elements in the list
      Returns:
      a new unmodifiable list instance
    • newUnmodifiableList

      public static <T> List<T> newUnmodifiableList(@NotNull @NotNull List<T> list)
      Creates a new unmodifiable list from the provided list. The method ensures that the returned list is immutable and changes to the original list will not reflect in the new unmodifiable list.
      Parameters:
      list - the list from which the unmodifiable list is to be created; must not be null
      Returns:
      an unmodifiable list containing the same elements as the provided list
    • newUnmodifiableList

      @SafeVarargs public static <T> List<T> newUnmodifiableList(@NotNull @NotNull T... elements)
      Creates a new unmodifiable list containing the specified elements.
      Type Parameters:
      T - the type of elements in the list
      Parameters:
      elements - the elements to include in the unmodifiable list; must not be null
      Returns:
      an unmodifiable list containing the provided elements
    • newArrayList

      public static <T> List<T> newArrayList(@org.jetbrains.annotations.Range(from=0L, to=2147483647L) int size)
      Creates a new List instance with the specified initial size. Depending on the configuration, it uses either a fastutil-backed implementation or a standard ArrayList.
      Type Parameters:
      T - The type of elements the list will contain.
      Parameters:
      size - The initial size of the list. Must be greater than or equal to 0.
      Returns:
      A new list instance with the specified size.
    • newArrayList

      @SafeVarargs public static <T> List<T> newArrayList(@NotNull @NotNull T... elements)
      Creates a new array-backed List containing the provided elements. The returned list may be an instance of a more specialized implementation depending on the runtime configuration.
      Type Parameters:
      T - the type of elements in the list
      Parameters:
      elements - the elements to include in the new list; must not be null
      Returns:
      a new List containing the specified elements
    • newArrayList

      public static <T> List<T> newArrayList(@NotNull @NotNull Collection<? extends T> elements)
      Creates a new list containing the elements from the specified collection.
      Parameters:
      elements - the collection whose elements are to be placed into the new list, must not be null
      Returns:
      a new list containing the elements from the specified collection
    • entryForEach

      public static <K, V> void entryForEach(Map<K,V> map, Consumer<? super Map.Entry<K,V>> consumer)
      Iterates over each entry in the provided map and applies the given consumer action to each entry. If the map is an instance of a fastutil Object2ObjectOpenHashMap, it uses optimized iteration, otherwise defaults to standard map entry iteration.
      Type Parameters:
      K - the type of keys maintained by the map
      V - the type of mapped values
      Parameters:
      map - the map whose entries are to be processed
      consumer - the action to be performed for each map entry
    • removeIf

      public static <K, V> boolean removeIf(Map<K,V> map, Predicate<? super Map.Entry<K,V>> filter)
      Removes all entries from the specified map that satisfy the provided predicate. If the map is an instance of Object2ObjectOpenHashMap from FastUtil, it utilizes the optimized removal method for that implementation; otherwise, it defaults to the standard Java map implementation.
      Parameters:
      map - the map from which entries are to be removed based on the given predicate
      filter - the predicate that tests each entry; entries that satisfy this predicate are removed
      Returns:
      true if any entries were removed from the map, otherwise false