Class Collections

java.lang.Object
com.saperion.common.lang.collection.Collections

public final class Collections extends Object
Utility class for collection treatment.
Author:
agz
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    areEqual(List<?> list1, List<?> list2)
    Checks if the two provided lists contain the same items in the same order.
    static <T> T[]
    array(T... elements)
    This is a convenience method - and actually just some syntactical sugar - to avoid java array literals.
    static boolean
    containSameEntries(Map<?,?> map1, Map<?,?> map2)
    Checks if the two provided maps contain the same entries.
    static boolean
    Checks if the two provided collections contain the same items.
    static int
    initialCapacity(int elementCount)
    This method returns the minimum initial capacity for maps that is suitable to hold the specified number of elements.
    static <T> CaseInsensitiveMap<T>
    newCaseInsensitiveMap(int elementCount)
    Creates a new CaseInsensitiveMap with a size suitable to hold at least the specified number of elements without resizing.
    static <T1, T2> HashMap<T1,T2>
    newHashMap(int elementCount)
    Creates a new HashMap with a size suitable to hold at least the specified number of elements without resizing.
    static <T1, T2> LinkedHashMap<T1,T2>
    newLinkedHashMap(int elementCount)
    Creates a new LinkedHashMap with a size suitable to hold at least the specified number of elements without resizing.

    Methods inherited from class java.lang.Object

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

    • newHashMap

      public static <T1, T2> HashMap<T1,T2> newHashMap(int elementCount)
      Creates a new HashMap with a size suitable to hold at least the specified number of elements without resizing.

      Background: The constructor of the JDK-HashMap accepts a size-argument that does NOT represent the number of elements the map can store. Instead it is the capacity of the internal hash-array. This array will never be filled completely, a resize will occur if the maximum load factor of the map is reached. But this load factor is not public. In summary, there is no way to initialize a map with the desired element count and the meaning of the initialization parameter remains completely unclear to the caller.

      This method solves these problems. It accepts the intended element count and creates a map of suitable size. It uses internal knowledge of the JDK, so if the implementation of the JDK should ever change, this method must be adapted accordingly.

      Type Parameters:
      T1 - type of the keys
      T2 - type of the values
      Parameters:
      elementCount - number of elements the new map should except without resizing
      Returns:
      new HashMap, suitable for the specified number of elements
    • newLinkedHashMap

      public static <T1, T2> LinkedHashMap<T1,T2> newLinkedHashMap(int elementCount)
      Creates a new LinkedHashMap with a size suitable to hold at least the specified number of elements without resizing.

      Background: The constructor of the JDK-LinkedHashMap accepts a size-argument that does NOT represent the number of elements the map can store. Instead it is the capacity of the internal hash-array. This array will never be filled completely, a resize will occur if the maximum load factor of the map is reached. But this load factor is not public. In summary, there is no way to initialize a map with the desired element count and the meaning of the initialization parameter remains completely unclear to the caller.

      This method solves these problems. It accepts the intended element count and creates a map of suitable size. It uses internal knowledge of the JDK, so if the implementation of the JDK should ever change, this method must be adapted accordingly.

      Type Parameters:
      T1 - type of the keys
      T2 - type of the values
      Parameters:
      elementCount - number of elements the new map should except without resizing
      Returns:
      new LinkedHashMap, suitable for the specified number of elements
    • newCaseInsensitiveMap

      public static <T> CaseInsensitiveMap<T> newCaseInsensitiveMap(int elementCount)
      Creates a new CaseInsensitiveMap with a size suitable to hold at least the specified number of elements without resizing.

      Background: The constructor of the CaseInsensitiveMap falls back on the constructor of the JDK-HashMap that accepts a size-argument that does NOT represent the number of elements the map can store. Instead it is the capacity of the internal hash-array. This array will never be filled completely, a resize will occur if the maximum load factor of the map is reached. But this load factor is not public. In summary, there is no way to initialize a map with the desired element count and the meaning of the initialization parameter remains completely unclear to the caller.

      This method solves these problems. It accepts the intended element count and creates a map of suitable size. It uses internal knowledge of the JDK, so if the implementation of the JDK should ever change, this method must be adapted accordingly.

      Type Parameters:
      T - type of the values
      Parameters:
      elementCount - number of elements the new map should except without resizing
      Returns:
      new HashMap, suitable for the specified number of elements
    • initialCapacity

      public static int initialCapacity(int elementCount)
      This method returns the minimum initial capacity for maps that is suitable to hold the specified number of elements.
      Parameters:
      elementCount - the number of elements to hold
      Returns:
      the minimum initial capacity for maps that is suitable to hold the specified number of elements
    • array

      public static <T> T[] array(T... elements)
      This is a convenience method - and actually just some syntactical sugar - to avoid java array literals. Instead of new T[]{t1, t2, t3} one can write Collections.array(t1, t2, t3)
      Type Parameters:
      T - the type of the elements
      Parameters:
      elements - the elements that form the array
      Returns:
      an array containing the given elements
    • containSameItems

      public static boolean containSameItems(Collection<?> col1, Collection<?> col2)
      Checks if the two provided collections contain the same items. Returns true if and only if:
      • both collections are null or
      • both collections are not null and
        • have the same size and
        • col2 contains all elements in col1
      The equality of the contained items is checked using the Collection.contains(Object) method. The order of the contained items is not checked.
      Parameters:
      col1 - the first collection to compare
      col2 - the second collection to compare
      Returns:
      true if the two collections contain the same items as defined above, otherwise false
    • areEqual

      public static boolean areEqual(List<?> list1, List<?> list2)
      Checks if the two provided lists contain the same items in the same order. Returns true if and only if:
      • both lists are null or
      • both lists are not null and
        • have the same size and
        • list2 contains all elements in list1 and
        • the order of the items is the same in both lists
      The equality of the contained items is checked using the Collection.contains(Object) method.
      Parameters:
      list1 - the first list to compare
      list2 - the second list to compare
      Returns:
      true if the two collections are equal as defined above, otherwise false
    • containSameEntries

      public static boolean containSameEntries(Map<?,?> map1, Map<?,?> map2)
      Checks if the two provided maps contain the same entries. Returns true if and only if:
      • both maps are null or
      • both maps are not null and
        • contan the same keys and
        • the keys are mapped to the same values in both maps
      The equality of the keys is checked using the Collection.contains(Object) method of the entry-set. The equality of the values is checked using the equals-method of the values. The order of the entries is not checked.
      Parameters:
      map1 - the first map to compare
      map2 - the second map to compare
      Returns:
      true when the maps contain the same entries as defined above, otherwise false