Class CollectionHelper
- java.lang.Object
-
- org.hibernate.internal.util.collections.CollectionHelper
-
public final class CollectionHelper extends Object
Various helper util methods for handling collections.
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_LIST_CAPACITY
static float
LOAD_FACTOR
static int
MINIMUM_INITIAL_CAPACITY
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
applyToProperties(Properties properties, Object... pairs)
static <T> ArrayList<T>
arrayList(int expectedNumberOfElements)
static <K,S>
Map<K,S>asMap(Object[] elements)
static String[]
asPairs(Map<String,String> map)
static Properties
asProperties(Map<?,?> map)
static <K,V>
voidcollectMapEntries(BiConsumer<K,V> mapEntryConsumer, Object[] mappings)
static <O> List<O>
combine(List<O>... lists)
static <O> List<O>
combine(List<O> list1, List<O> list2)
static <K,V>
ConcurrentHashMap<K,V>concurrentMap(int expectedNumberOfElements)
Create a properly sizedConcurrentHashMap
based on the given expected number of elements.static <K,V>
ConcurrentHashMap<K,V>concurrentMap(int expectedNumberOfElements, float loadFactor)
Create a properly sizedConcurrentHashMap
based on the given expected number of elements and an explicit load factorstatic int
determineProperSizing(int numberOfElements)
Determine the proper initial size for a new collection in order for it to hold the given a number of elements.static int
determineProperSizing(Map original)
Given a map, determine the proper initial size for a new Map to hold the same number of values.static int
determineProperSizing(Set original)
Given a set, determine the proper initial size for a new set to hold the same number of values.static boolean
isEmpty(Object[] objects)
static boolean
isEmpty(Collection collection)
static boolean
isEmpty(Map map)
static boolean
isNotEmpty(Object[] objects)
static boolean
isNotEmpty(Collection collection)
static boolean
isNotEmpty(Map map)
static <K,V>
LinkedHashMap<K,V>linkedMap()
Build a linked map whose size is unknown.static <K,V>
LinkedHashMap<K,V>linkedMapOfSize(int size)
Build a properly sized linked map, especially handling load size and load factor to prevent immediate resizing.static <K> LinkedHashSet<K>
linkedSet()
Build a linked set whose size is unknown.static <K> LinkedHashSet<K>
linkedSetOfSize(int size)
Build a properly sized linked set, especially handling load size and load factor to prevent immediate resizing.static <T> List<T>
listOf(T value1)
static <T> List<T>
listOf(T... values)
static <K,V>
HashMap<K,V>makeCopy(Map<K,V> original, Function<K,K> keyTransformer, Function<V,V> valueTransformer)
static <X,Y>
Map<X,Y>makeCopy(Map<X,Y> map)
static <T> Set<T>
makeCopy(Set<T> source)
static <K,V,E>
Map<K,V>makeMap(Collection<E> collection, Function<E,K> keyProducer, Function<E,V> valueProducer)
static <K,V>
Map<K,V>makeMap(Collection<V> collection, Function<V,K> keyProducer)
static <K,V>
HashMap<K,V>map()
Build a map whose size is unknown.static <K,V>
HashMap<K,V>mapOfSize(int size)
Build a properly sized map, especially handling load size and load factor to prevent immediate resizing.static <K> HashSet<K>
set()
Build a set whose size is unknown.static <T> Set<T>
setOf(T... values)
static <K> HashSet<K>
setOfSize(int size)
Build a properly sized set, especially handling load size and load factor to prevent immediate resizing.static int
size(Collection<?> values)
static int
size(List<?> values)
static int
size(Map<?,?> values)
static Map<String,?>
toMap(Object... pairs)
static Map<String,String>
toMap(String... pairs)
static Properties
toProperties(Object... pairs)
static <X> Set<X>
toSet(X... values)
static Map<String,Object>
toSettingsMap(Object... pairs)
static <V> List<V>
toSmallList(ArrayList<V> arrayList)
Use to convert ArrayList instances which will be retained for a long time, such as for the lifetime of the Hibernate ORM instance.static <K,V>
Map<K,V>toSmallMap(Map<K,V> map)
Use to convert Maps which will be retained for a long time, such as for the lifetime of the Hibernate ORM instance.static <T> Set<T>
toSmallSet(Set<T> set)
Use to convert sets which will be retained for a long time, such as for the lifetime of the Hibernate ORM instance.
-
-
-
Field Detail
-
DEFAULT_LIST_CAPACITY
public static final int DEFAULT_LIST_CAPACITY
- See Also:
- Constant Field Values
-
MINIMUM_INITIAL_CAPACITY
public static final int MINIMUM_INITIAL_CAPACITY
- See Also:
- Constant Field Values
-
LOAD_FACTOR
public static final float LOAD_FACTOR
- See Also:
- Constant Field Values
-
-
Method Detail
-
mapOfSize
public static <K,V> HashMap<K,V> mapOfSize(int size)
Build a properly sized map, especially handling load size and load factor to prevent immediate resizing.Especially helpful for copy map contents.
- Parameters:
size
- The size to make the map.- Returns:
- The sized map.
-
linkedMapOfSize
public static <K,V> LinkedHashMap<K,V> linkedMapOfSize(int size)
Build a properly sized linked map, especially handling load size and load factor to prevent immediate resizing.Especially helpful for copy map contents.
- Parameters:
size
- The size to make the map.- Returns:
- The sized linked map.
-
map
public static <K,V> HashMap<K,V> map()
Build a map whose size is unknown.- Returns:
- The map.
-
linkedMap
public static <K,V> LinkedHashMap<K,V> linkedMap()
Build a linked map whose size is unknown.- Returns:
- The linked map.
-
setOfSize
public static <K> HashSet<K> setOfSize(int size)
Build a properly sized set, especially handling load size and load factor to prevent immediate resizing.Especially helpful for copy set contents.
- Parameters:
size
- The size to make the set.- Returns:
- The sized set.
-
set
public static <K> HashSet<K> set()
Build a set whose size is unknown.- Returns:
- The set.
-
linkedSetOfSize
public static <K> LinkedHashSet<K> linkedSetOfSize(int size)
Build a properly sized linked set, especially handling load size and load factor to prevent immediate resizing.Especially helpful for copy set contents.
- Parameters:
size
- The size to make the set.- Returns:
- The sized linked set.
-
linkedSet
public static <K> LinkedHashSet<K> linkedSet()
Build a linked set whose size is unknown.- Returns:
- The linked set.
-
determineProperSizing
public static int determineProperSizing(Map original)
Given a map, determine the proper initial size for a new Map to hold the same number of values. Specifically we want to account for load size and load factor to prevent immediate resizing.- Parameters:
original
- The original map- Returns:
- The proper size.
-
makeCopy
public static <K,V> HashMap<K,V> makeCopy(Map<K,V> original, Function<K,K> keyTransformer, Function<V,V> valueTransformer)
-
makeMap
public static <K,V> Map<K,V> makeMap(Collection<V> collection, Function<V,K> keyProducer)
-
makeMap
public static <K,V,E> Map<K,V> makeMap(Collection<E> collection, Function<E,K> keyProducer, Function<E,V> valueProducer)
-
determineProperSizing
public static int determineProperSizing(Set original)
Given a set, determine the proper initial size for a new set to hold the same number of values. Specifically we want to account for load size and load factor to prevent immediate resizing.- Parameters:
original
- The original set- Returns:
- The proper size.
-
determineProperSizing
public static int determineProperSizing(int numberOfElements)
Determine the proper initial size for a new collection in order for it to hold the given a number of elements. Specifically we want to account for load size and load factor to prevent immediate resizing.- Parameters:
numberOfElements
- The number of elements to be stored.- Returns:
- The proper size.
-
concurrentMap
public static <K,V> ConcurrentHashMap<K,V> concurrentMap(int expectedNumberOfElements)
Create a properly sizedConcurrentHashMap
based on the given expected number of elements.- Type Parameters:
K
- The map key typeV
- The map value type- Parameters:
expectedNumberOfElements
- The expected number of elements for the created map- Returns:
- The created map.
-
concurrentMap
public static <K,V> ConcurrentHashMap<K,V> concurrentMap(int expectedNumberOfElements, float loadFactor)
Create a properly sizedConcurrentHashMap
based on the given expected number of elements and an explicit load factor- Type Parameters:
K
- The map key typeV
- The map value type- Parameters:
expectedNumberOfElements
- The expected number of elements for the created maploadFactor
- The collection load factor- Returns:
- The created map.
-
arrayList
public static <T> ArrayList<T> arrayList(int expectedNumberOfElements)
-
isEmpty
public static boolean isEmpty(Collection collection)
-
isEmpty
public static boolean isEmpty(Map map)
-
isNotEmpty
public static boolean isNotEmpty(Collection collection)
-
isNotEmpty
public static boolean isNotEmpty(Map map)
-
isEmpty
public static boolean isEmpty(Object[] objects)
-
isNotEmpty
public static boolean isNotEmpty(Object[] objects)
-
listOf
public static <T> List<T> listOf(T value1)
-
listOf
public static <T> List<T> listOf(T... values)
-
setOf
public static <T> Set<T> setOf(T... values)
-
asProperties
public static Properties asProperties(Map<?,?> map)
-
toSmallSet
public static <T> Set<T> toSmallSet(Set<T> set)
Use to convert sets which will be retained for a long time, such as for the lifetime of the Hibernate ORM instance. The returned Set might be immutable, but there is no guarantee of this: consider it immutable but don't rely on this. The goal is to save memory.- Returns:
- will never return null, but might return an immutable collection.
-
toSmallMap
public static <K,V> Map<K,V> toSmallMap(Map<K,V> map)
Use to convert Maps which will be retained for a long time, such as for the lifetime of the Hibernate ORM instance. The returned Map might be immutable, but there is no guarantee of this: consider it immutable but don't rely on this. The goal is to save memory.
-
toSmallList
public static <V> List<V> toSmallList(ArrayList<V> arrayList)
Use to convert ArrayList instances which will be retained for a long time, such as for the lifetime of the Hibernate ORM instance. The returned List might be immutable, but there is no guarantee of this: consider it immutable but don't rely on this. The goal is to save memory.
-
collectMapEntries
public static <K,V> void collectMapEntries(BiConsumer<K,V> mapEntryConsumer, Object[] mappings)
-
toProperties
public static Properties toProperties(Object... pairs)
-
applyToProperties
public static void applyToProperties(Properties properties, Object... pairs)
-
size
public static int size(List<?> values)
-
size
public static int size(Collection<?> values)
-
size
public static int size(Map<?,?> values)
-
toSet
public static <X> Set<X> toSet(X... values)
-
-