Top Description Fields Constructors Methods
org.hibernate.internal.util.collections

public final Class CollectionHelper

extends Object
Class Inheritance
Imports
java.util.ArrayList, .Arrays, .Collection, .Collections, .HashMap, .HashSet, .LinkedHashMap, .LinkedHashSet, .List, .Map, .Properties, .Set, java.util.concurrent.ConcurrentHashMap, java.util.function.BiConsumer, .Function

Various helper util methods for handling collections.
Authors
Gavin King, Steve Ebersole

Field Summary

Modifier and TypeField and Description
public static final int
public static final float
public static final int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static void
applyToMap(Map<String, String> map, String... pairs)

private static void
applyToMap(Map<String, ?> map, Object... pairs)

public static void
applyToProperties(Properties properties, Object... pairs)

public static <T> ArrayList<T>
arrayList(int expectedNumberOfElements)

public static <K, S> Map<K, S>
asMap(Object[] elements)

public static String[]
public static Properties
asProperties(Map<?, ?> map)

public static <K, V> void
collectMapEntries(BiConsumer<K, V> mapEntryConsumer, Object[] mappings)

public static <O> List<O>
combine(List<O> list1, List<O> list2)

public static <O> List<O>
combine(List<O>... lists)

public static <
The map key type
K
,
The map value type
V
>
ConcurrentHashMap<K, V>

Returns:

The created map.
concurrentMap
(int
The expected number of elements for the created map
expectedNumberOfElements
)

Create a properly sized ConcurrentHashMap based on the given expected number of elements.

public static <
The map key type
K
,
The map value type
V
>
ConcurrentHashMap<K, V>

Returns:

The created map.
concurrentMap
(int
The expected number of elements for the created map
expectedNumberOfElements
,
float
The collection load factor
loadFactor
)

Create a properly sized ConcurrentHashMap based on the given expected number of elements and an explicit load factor

public static int

Returns:

The proper size.
determineProperSizing
(Map<K, V>
The original map
original
)

Given a map, determine the proper initial size for a new Map to hold the same number of values.

public static int

Returns:

The proper size.
determineProperSizing
(Set<E>
The original set
original
)

Given a set, determine the proper initial size for a new set to hold the same number of values.

public static int

Returns:

The proper size.
determineProperSizing
(int
The number of elements to be stored.
numberOfElements
)

Determine the proper initial size for a new collection in order for it to hold the given a number of elements.

public static boolean
isEmpty(Collection<E> collection)

public static boolean
isEmpty(Map<K, V> map)

public static boolean
isEmpty(Object[] objects)

public static boolean
isNotEmpty(Collection<E> collection)

public static boolean
isNotEmpty(Map<K, V> map)

public static boolean
isNotEmpty(Object[] objects)

public static <K, V> LinkedHashMap<K, V>

Returns:

The linked map.
linkedMap
()

Build a linked map whose size is unknown.

public static <K, V> LinkedHashMap<K, V>

Returns:

The sized linked map.
linkedMapOfSize
(int
The size to make the map.
size
)

Build a properly sized linked map, especially handling load size and load factor to prevent immediate resizing.

public static <K> LinkedHashSet<K>

Returns:

The linked set.
linkedSet
()

Build a linked set whose size is unknown.

public static <K> LinkedHashSet<K>

Returns:

The sized linked set.
linkedSetOfSize
(int
The size to make the set.
size
)

Build a properly sized linked set, especially handling load size and load factor to prevent immediate resizing.

public static <T> List<T>
listOf(T value1)

public static <T> List<T>
listOf(T... values)

public static <X, Y> Map<X, Y>
makeCopy(Map<X, Y> map)

public static <K, V> HashMap<K, V>
makeCopy(Map<K, V> original, Function<K, K> keyTransformer, Function<V, V> valueTransformer)

public static <T> Set<T>
makeCopy(Set<T> source)

public static <K, V> Map<K, V>
makeMap(Collection<V> collection, Function<V, K> keyProducer)

public static <K, V, E> Map<K, V>
makeMap(Collection<E> collection, Function<E, K> keyProducer, Function<E, V> valueProducer)

public static <K, V> HashMap<K, V>

Returns:

The map.
map
()

Build a map whose size is unknown.

public static <K, V> HashMap<K, V>

Returns:

The sized map.
mapOfSize
(int
The size to make the map.
size
)

Build a properly sized map, especially handling load size and load factor to prevent immediate resizing.

public static <K> HashSet<K>

Returns:

The set.
set
()

Build a set whose size is unknown.

public static <T> Set<T>
setOf(T... values)

public static <K> HashSet<K>

Returns:

The sized set.
setOfSize
(int
The size to make the set.
size
)

Build a properly sized set, especially handling load size and load factor to prevent immediate resizing.

public static int
size(List<?> values)

public static int
size(Collection<?> values)

public static int
size(Map<?, ?> values)

public static Map<String, String>
toMap(String... pairs)

public static Map<String, ?>
toMap(Object... pairs)

public static Properties
toProperties(Object... pairs)

public static <X> Set<X>
toSet(X... values)

public static Map<String, Object>
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.

public static <K, V> Map<K, V>
toSmallMap(final 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.

public static <T> Set<T>

Returns:

will never return null, but might return an immutable collection.
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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_LIST_CAPACITYback to summary
public static final int DEFAULT_LIST_CAPACITY
LOAD_FACTORback to summary
public static final float LOAD_FACTOR
MINIMUM_INITIAL_CAPACITYback to summary
public static final int MINIMUM_INITIAL_CAPACITY

Constructor Detail

CollectionHelperback to summary
private CollectionHelper()

Method Detail

applyToMapback to summary
private static void applyToMap(Map<String, String> map, String... pairs)
applyToMapback to summary
private static void applyToMap(Map<String, ?> map, Object... pairs)
Annotations
@SuppressWarnings:unchecked, rawtypes
applyToPropertiesback to summary
public static void applyToProperties(Properties properties, Object... pairs)
arrayListback to summary
public static <T> ArrayList<T> arrayList(int expectedNumberOfElements)
asMapback to summary
public static <K, S> Map<K, S> asMap(Object[] elements)
Annotations
@SuppressWarnings:unchecked
asPairsback to summary
public static String[] asPairs(Map<String, String> map)
asPropertiesback to summary
public static Properties asProperties(Map<?, ?> map)
collectMapEntriesback to summary
public static <K, V> void collectMapEntries(BiConsumer<K, V> mapEntryConsumer, Object[] mappings)
Annotations
@SuppressWarnings:unchecked
combineback to summary
public static <O> List<O> combine(List<O> list1, List<O> list2)
combineback to summary
public static <O> List<O> combine(List<O>... lists)
concurrentMapback to summary
public static <K, V> ConcurrentHashMap<K, V> concurrentMap(int expectedNumberOfElements)

Create a properly sized ConcurrentHashMap based on the given expected number of elements.

Parameters
<K>
The map key type
<V>
The map value type
expectedNumberOfElements:int

The expected number of elements for the created map

Returns:ConcurrentHashMap<K, V>

The created map.

concurrentMapback to summary
public static <K, V> ConcurrentHashMap<K, V> concurrentMap(int expectedNumberOfElements, float loadFactor)

Create a properly sized ConcurrentHashMap based on the given expected number of elements and an explicit load factor

Parameters
<K>
The map key type
<V>
The map value type
expectedNumberOfElements:int

The expected number of elements for the created map

loadFactor:float

The collection load factor

Returns:ConcurrentHashMap<K, V>

The created map.

determineProperSizingback to summary
public static int determineProperSizing(Map<K, V> 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:Map<K, V>

The original map

Returns:int

The proper size.

determineProperSizingback to summary
public static int determineProperSizing(Set<E> 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:Set<E>

The original set

Returns:int

The proper size.

determineProperSizingback to summary
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:int

The number of elements to be stored.

Returns:int

The proper size.

isEmptyback to summary
public static boolean isEmpty(Collection<E> collection)
isEmptyback to summary
public static boolean isEmpty(Map<K, V> map)
isEmptyback to summary
public static boolean isEmpty(Object[] objects)
isNotEmptyback to summary
public static boolean isNotEmpty(Collection<E> collection)
isNotEmptyback to summary
public static boolean isNotEmpty(Map<K, V> map)
isNotEmptyback to summary
public static boolean isNotEmpty(Object[] objects)
linkedMapback to summary
public static <K, V> LinkedHashMap<K, V> linkedMap()

Build a linked map whose size is unknown.

Returns:LinkedHashMap<K, V>

The linked map.

linkedMapOfSizeback to summary
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:int

The size to make the map.

Returns:LinkedHashMap<K, V>

The sized linked map.

linkedSetback to summary
public static <K> LinkedHashSet<K> linkedSet()

Build a linked set whose size is unknown.

Returns:LinkedHashSet<K>

The linked set.

linkedSetOfSizeback to summary
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:int

The size to make the set.

Returns:LinkedHashSet<K>

The sized linked set.

listOfback to summary
public static <T> List<T> listOf(T value1)
listOfback to summary
public static <T> List<T> listOf(T... values)
makeCopyback to summary
public static <X, Y> Map<X, Y> makeCopy(Map<X, Y> map)
makeCopyback to summary
public static <K, V> HashMap<K, V> makeCopy(Map<K, V> original, Function<K, K> keyTransformer, Function<V, V> valueTransformer)
makeCopyback to summary
public static <T> Set<T> makeCopy(Set<T> source)
makeMapback to summary
public static <K, V> Map<K, V> makeMap(Collection<V> collection, Function<V, K> keyProducer)
makeMapback to summary
public static <K, V, E> Map<K, V> makeMap(Collection<E> collection, Function<E, K> keyProducer, Function<E, V> valueProducer)
mapback to summary
public static <K, V> HashMap<K, V> map()

Build a map whose size is unknown.

Returns:HashMap<K, V>

The map.

mapOfSizeback to summary
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:int

The size to make the map.

Returns:HashMap<K, V>

The sized map.

setback to summary
public static <K> HashSet<K> set()

Build a set whose size is unknown.

Returns:HashSet<K>

The set.

setOfback to summary
public static <T> Set<T> setOf(T... values)
setOfSizeback to summary
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:int

The size to make the set.

Returns:HashSet<K>

The sized set.

sizeback to summary
public static int size(List<?> values)
sizeback to summary
public static int size(Collection<?> values)
sizeback to summary
public static int size(Map<?, ?> values)
toMapback to summary
public static Map<String, String> toMap(String... pairs)
toMapback to summary
public static Map<String, ?> toMap(Object... pairs)
toPropertiesback to summary
public static Properties toProperties(Object... pairs)
toSetback to summary
public static <X> Set<X> toSet(X... values)
toSettingsMapback to summary
public static Map<String, Object> toSettingsMap(Object... pairs)
toSmallListback to summary
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.

toSmallMapback to summary
public static <K, V> Map<K, V> toSmallMap(final 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.

toSmallSetback to summary
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:Set<T>

will never return null, but might return an immutable collection.