Package org.apache.flink.util
Class CollectionUtil
- java.lang.Object
-
- org.apache.flink.util.CollectionUtil
-
-
Field Summary
Fields Modifier and Type Field Description static int
MAX_ARRAY_SIZE
A safe maximum size for arrays in the JVM.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Collection<T>
checkedSubTypeCast(Collection<? super T> collection, Class<T> subTypeClass)
Casts the given collection to a subtype.static <K,V>
Map.Entry<K,V>entry(K k, V v)
Returns an immutableMap.Entry
.static boolean
isEmptyOrAllElementsNull(Collection<?> collection)
Returns true if the given collection is empty or contains only null elements.static boolean
isNullOrEmpty(Collection<?> collection)
Returns true if the given collection is null or empty.static boolean
isNullOrEmpty(Map<?,?> map)
static <E> List<E>
iterableToList(Iterable<E> iterable)
Collects the elements in the Iterable in a List.static <E> List<E>
iteratorToList(Iterator<E> iterator)
Collects the elements in the Iterator in a List.static <K,V>
Map<K,V>map(Map.Entry<K,V>... entries)
Returns an immutableMap
from the provided entries.static <T,R>
Stream<R>mapWithIndex(Collection<T> input, BiFunction<T,Integer,R> mapper)
static <K,V>
HashMap<K,V>newHashMapWithExpectedSize(int expectedSize)
Creates a newHashMap
of the expected size, i.e. a hash map that will not rehash if expectedSize many keys are inserted, considering the load factor.static <E> HashSet<E>
newHashSetWithExpectedSize(int expectedSize)
Creates a newHashSet
of the expected size, i.e. a hash set that will not rehash if expectedSize many unique elements are inserted, considering the load factor.static <K,V>
LinkedHashMap<K,V>newLinkedHashMapWithExpectedSize(int expectedSize)
Creates a newLinkedHashMap
of the expected size, i.e. a hash map that will not rehash if expectedSize many keys are inserted, considering the load factor.static <E> LinkedHashSet<E>
newLinkedHashSetWithExpectedSize(int expectedSize)
Creates a newLinkedHashSet
of the expected size, i.e. a hash set that will not rehash if expectedSize many unique elements are inserted, considering the load factor.static <T> Set<T>
ofNullable(T obj)
static <T> Collection<List<T>>
partition(Collection<T> elements, int numBuckets)
Partition a collection into approximately n buckets.static <I,O>
Collection<O>project(Collection<I> collection, Function<I,O> projector)
static <T> Collection<T>
subTypeCast(Collection<? super T> collection)
Casts the given collection to a subtype.
-
-
-
Field Detail
-
MAX_ARRAY_SIZE
public static final int MAX_ARRAY_SIZE
A safe maximum size for arrays in the JVM.- See Also:
- Constant Field Values
-
-
Method Detail
-
isNullOrEmpty
public static boolean isNullOrEmpty(Collection<?> collection)
Returns true if the given collection is null or empty.
-
isEmptyOrAllElementsNull
public static boolean isEmptyOrAllElementsNull(Collection<?> collection)
Returns true if the given collection is empty or contains only null elements.
-
isNullOrEmpty
public static boolean isNullOrEmpty(Map<?,?> map)
-
ofNullable
public static <T> Set<T> ofNullable(@Nullable T obj)
-
mapWithIndex
public static <T,R> Stream<R> mapWithIndex(Collection<T> input, BiFunction<T,Integer,R> mapper)
-
partition
public static <T> Collection<List<T>> partition(Collection<T> elements, int numBuckets)
Partition a collection into approximately n buckets.
-
project
public static <I,O> Collection<O> project(Collection<I> collection, Function<I,O> projector)
-
iterableToList
public static <E> List<E> iterableToList(@Nullable Iterable<E> iterable)
Collects the elements in the Iterable in a List. If the iterable argument is null, this method returns an empty list.
-
iteratorToList
public static <E> List<E> iteratorToList(@Nullable Iterator<E> iterator)
Collects the elements in the Iterator in a List. If the iterator argument is null, this method returns an empty list.
-
map
@SafeVarargs public static <K,V> Map<K,V> map(Map.Entry<K,V>... entries)
Returns an immutableMap
from the provided entries.
-
newHashMapWithExpectedSize
public static <K,V> HashMap<K,V> newHashMapWithExpectedSize(int expectedSize)
Creates a newHashMap
of the expected size, i.e. a hash map that will not rehash if expectedSize many keys are inserted, considering the load factor.- Type Parameters:
K
- the type of keys maintained by this map.V
- the type of mapped values.- Parameters:
expectedSize
- the expected size of the created hash map.- Returns:
- a new hash map instance with enough capacity for the expected size.
-
newLinkedHashMapWithExpectedSize
public static <K,V> LinkedHashMap<K,V> newLinkedHashMapWithExpectedSize(int expectedSize)
Creates a newLinkedHashMap
of the expected size, i.e. a hash map that will not rehash if expectedSize many keys are inserted, considering the load factor.- Type Parameters:
K
- the type of keys maintained by this map.V
- the type of mapped values.- Parameters:
expectedSize
- the expected size of the created hash map.- Returns:
- a new hash map instance with enough capacity for the expected size.
-
newHashSetWithExpectedSize
public static <E> HashSet<E> newHashSetWithExpectedSize(int expectedSize)
Creates a newHashSet
of the expected size, i.e. a hash set that will not rehash if expectedSize many unique elements are inserted, considering the load factor.- Type Parameters:
E
- the type of elements stored by this set.- Parameters:
expectedSize
- the expected size of the created hash map.- Returns:
- a new hash map instance with enough capacity for the expected size.
-
newLinkedHashSetWithExpectedSize
public static <E> LinkedHashSet<E> newLinkedHashSetWithExpectedSize(int expectedSize)
Creates a newLinkedHashSet
of the expected size, i.e. a hash set that will not rehash if expectedSize many unique elements are inserted, considering the load factor.- Type Parameters:
E
- the type of elements stored by this set.- Parameters:
expectedSize
- the expected size of the created hash map.- Returns:
- a new hash map instance with enough capacity for the expected size.
-
subTypeCast
public static <T> Collection<T> subTypeCast(Collection<? super T> collection)
Casts the given collection to a subtype. This is an unchecked cast that can lead to runtime exceptions.- Type Parameters:
T
- the subtype to cast to.- Parameters:
collection
- the collection to cast.- Returns:
- the collection unchecked-cast to a subtype.
-
checkedSubTypeCast
public static <T> Collection<T> checkedSubTypeCast(Collection<? super T> collection, Class<T> subTypeClass)
Casts the given collection to a subtype. This is a checked cast.- Type Parameters:
T
- the subtype to cast to.- Parameters:
collection
- the collection to cast.subTypeClass
- the class of the subtype to cast to.- Returns:
- the collection checked and cast to a subtype.
-
-