Package org.apache.druid.utils
Class CollectionUtils
- java.lang.Object
-
- org.apache.druid.utils.CollectionUtils
-
public final class CollectionUtils extends Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <E> Collection<E>
createLazyCollectionFromStream(Supplier<Stream<E>> sequentialStreamSupplier, int size)
Returns a lazy collection from a stream supplier and a size.static <T,I extends Iterable<T>,X extends Throwable>
TgetOnlyElement(I iterable, Function<? super I,? extends X> exceptionSupplier)
LikeIterables.getOnlyElement(Iterable)
, but allows a customizable error message.static <T> Set<T>
intersect(Set<T> left, Set<T> right)
Intersection of two sets:C = A ∩ B
.static boolean
isNullOrEmpty(Collection<?> list)
static <K,V,K2>
Map<K2,V>mapKeys(Map<K,V> map, Function<K,K2> keyMapper)
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>mapValues(Map<K,V> map, Function<V,V2> valueMapper)
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(Set<T> left, Set<T> right)
Subtract one set from another:C = A - B
.static <T> Set<T>
union(Set<T> left, Set<T> right)
Intersection of two sets:C = A ∪ B
.
-
-
-
Method Detail
-
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
public static <E> TreeSet<E> newTreeSet(Comparator<? super E> comparator, Iterable<E> elements)
-
mapValues
public static <K,V,V2> Map<K,V2> mapValues(Map<K,V> map, Function<V,V2> valueMapper)
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
public static <K,V,K2> Map<K2,V> mapKeys(Map<K,V> map, Function<K,K2> keyMapper)
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
-
newLinkedHashMapWithExpectedSize
public static <K,V> LinkedHashMap<K,V> newLinkedHashMapWithExpectedSize(int expectedSize)
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
public static boolean isNullOrEmpty(@Nullable Collection<?> list)
-
subtract
public static <T> Set<T> subtract(Set<T> left, Set<T> right)
Subtract one set from another:C = A - B
.
-
intersect
public static <T> Set<T> intersect(Set<T> left, Set<T> right)
Intersection of two sets:C = A ∩ B
.
-
union
public static <T> Set<T> union(Set<T> left, Set<T> right)
Intersection of two sets:C = A ∪ B
.
-
-