Package org.apache.druid.utils
Class CollectionUtils
java.lang.Object
org.apache.druid.utils.CollectionUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic <E> Collection<E>createLazyCollectionFromStream(Supplier<Stream<E>> sequentialStreamSupplier, int size) Returns a lazy collection from a stream supplier and a size.getOnlyElement(I iterable, Function<? super I, ? extends X> exceptionSupplier) LikeIterables.getOnlyElement(Iterable), but allows a customizable error message.static <T> Set<T>Intersection of two sets:C = A ∩ B.static booleanisNullOrEmpty(Collection<?> list) static <K,V, K2> Map<K2, V> Returns a transformed map from the given input map where the key is modified based on the given keyMapper function.static <K,V, V2> Map<K, V2> Returns a transformed map from the given input map where the value is modified based on the given valueMapper function.static <K,V> LinkedHashMap<K, V> newLinkedHashMapWithExpectedSize(int expectedSize) Returns a LinkedHashMap with an appropriate size based on the callers expectedSize.static <E> TreeSet<E>newTreeSet(Comparator<? super E> comparator, Iterable<E> elements) static <T> Set<T>Subtract one set from another:C = A - B.static <E,K, V> Map<K, V> toMap(Collection<E> collection, Function<E, K> keyMapper, Function<E, V> valueMapper) Creates an immutable map by mapping each entry in the given collection to a key and a value.static <T> Set<T>Union of two sets:C = A ∪ B.
-
Method Details
-
createLazyCollectionFromStream
public static <E> Collection<E> createLazyCollectionFromStream(Supplier<Stream<E>> sequentialStreamSupplier, int size) Returns a lazy collection from a stream supplier and a size.Collection.iterator()of the returned collection delegates toBaseStream.iterator()on the stream returned from the supplier. -
newTreeSet
-
mapValues
Returns a transformed map from the given input map where the value is modified based on the given valueMapper function. UnlikeMaps.transformValues(java.util.Map<K, V1>, com.google.common.base.Function<? super V1, V2>), this method applies the mapping function eagerly to all key-value pairs in the source map and returns a newHashMap, whileMaps.transformValues(java.util.Map<K, V1>, com.google.common.base.Function<? super V1, V2>)returns a lazy map view. -
mapKeys
Returns a transformed map from the given input map where the key is modified based on the given keyMapper function. This method fails if keys collide after applying the given keyMapper function and throws a IllegalStateException.- Throws:
ISE- if key collisions occur while applying specified keyMapper
-
toMap
public static <E,K, Map<K,V> V> toMap(Collection<E> collection, Function<E, K> keyMapper, Function<E, V> valueMapper) Creates an immutable map by mapping each entry in the given collection to a key and a value. -
newLinkedHashMapWithExpectedSize
Returns a LinkedHashMap with an appropriate size based on the callers expectedSize. This methods functionality mirrors that of com.google.common.collect.Maps#newLinkedHashMapWithExpectedSize in Guava 19+. Thus, this method can be replaced with Guava's implementation once Druid has upgraded its Guava dependency to a sufficient version.- Parameters:
expectedSize- the expected size of the LinkedHashMap- Returns:
- LinkedHashMap object with appropriate size based on callers expectedSize
-
isNullOrEmpty
-
subtract
Subtract one set from another:C = A - B. -
intersect
Intersection of two sets:C = A ∩ B. -
union
Union of two sets:C = A ∪ B. -
getOnlyElement
public static <T,I extends Iterable<T>, T getOnlyElementX extends Throwable> (I iterable, Function<? super I, ? extends X> exceptionSupplier) throws XLikeIterables.getOnlyElement(Iterable), but allows a customizable error message.- Throws:
X extends Throwable
-