Class CollectionUtils

java.lang.Object
org.apache.druid.utils.CollectionUtils

public final class CollectionUtils extends Object
  • 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 to BaseStream.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. Unlike Maps.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 new HashMap, while Maps.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
    • toMap

      public 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.
    • 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)
      Union of two sets: C = A ∪ B.
    • getOnlyElement

      public static <T, I extends Iterable<T>, X extends Throwable> T getOnlyElement(I iterable, Function<? super I,? extends X> exceptionSupplier) throws X
      Like Iterables.getOnlyElement(Iterable), but allows a customizable error message.
      Throws:
      X