Class Collections
java.lang.Object
com.saperion.common.lang.collection.Collections
Utility class for collection treatment.
- Author:
- agz
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
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
containSameItems
(Collection<?> col1, Collection<?> col2) 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 newCaseInsensitiveMap
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 newHashMap
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 newLinkedHashMap
with a size suitable to hold at least the specified number of elements without resizing.
-
Method Details
-
newHashMap
Creates a newHashMap
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 keysT2
- 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
Creates a newLinkedHashMap
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 keysT2
- 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
Creates a newCaseInsensitiveMap
with a size suitable to hold at least the specified number of elements without resizing. Background: The constructor of theCaseInsensitiveMap
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 ofnew T[]{t1, t2, t3}
one can writeCollections.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
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
Collection.contains(Object)
method. The order of the contained items is not checked.- Parameters:
col1
- the first collection to comparecol2
- the second collection to compare- Returns:
- true if the two collections contain the same items as defined above, otherwise false
-
areEqual
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
Collection.contains(Object)
method.- Parameters:
list1
- the first list to comparelist2
- the second list to compare- Returns:
- true if the two collections are equal as defined above, otherwise false
-
containSameEntries
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
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 comparemap2
- the second map to compare- Returns:
- true when the maps contain the same entries as defined above, otherwise false
-