Class CollectionUtil

java.lang.Object
org.eclipse.net4j.util.collection.CollectionUtil

public final class CollectionUtil extends Object
Various static helper methods.
Since:
3.5
Author:
Eike Stepper
  • Method Details

    • close

      public static void close(Object... closeables)
      Since:
      3.29
    • dump

      public static <T> Iterator<T> dump(Iterator<T> it)
    • topologicalSort

      public static <NODE> List<NODE> topologicalSort(Collection<NODE> nodes, Function<NODE,Collection<NODE>> edgeProvider)
      Since:
      3.25
    • topologicalSort

      public static <NODE> List<NODE> topologicalSort(Collection<NODE> nodes, Function<NODE,Collection<NODE>> edgeProvider, boolean reverse)
      Since:
      3.25
    • addNotNull

      public static <T> boolean addNotNull(Collection<? super T> c, T e)
      Since:
      3.16
    • setOf

      public static <T> Set<T> setOf(Collection<? extends T> c)
      Since:
      3.16
    • first

      public static <T> T first(Collection<? extends T> c)
      Since:
      3.16
    • removeAll

      public static <K, V> List<K> removeAll(Map<K,V> map, BiPredicate<K,V> predicate)
      Since:
      3.16
    • compute

      public static <K, V> V compute(Map<K,V> map, K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
      Since:
      3.16
    • subclasses

      public static <SUBTYPE, TYPE extends SUBTYPE> Function<? super SUBTYPE,? extends Stream<? extends TYPE>> subclasses(Class<TYPE> clazz)
      Returns a function that returns a stream containing the given object if it is an instance of the given class; otherwise an empty stream.

      Example usage:

       Stream<Animal> animals = Stream.of(new Dog(), new Cat(), new Bird());
       Stream<Dog> dogs = animals.flatMap(CollectionUtil.subclasses(Dog.class));
       

      This example creates a stream of animals and then uses the subclasses function to filter and cast the stream to only include instances of Dog. This is a little easier to read and remember than using filter and map separately, as the following equivalent code shows:

       Stream<Dog> dogs = animals
          .filter(Dog.class::isInstance)
          .map(Dog.class::cast);
       
      Since:
      3.29
    • stream

      public static <T> Stream<T> stream(Iterator<T> it)
      Returns a stream over the elements of the given iterator.
      Since:
      3.29
    • concat

      @SafeVarargs public static <T> Stream<T> concat(Stream<T>... streams)
      Returns a stream that concatenates the given streams.
      Since:
      3.26
    • concat

      public static <T> Stream<T> concat(Collection<Stream<T>> streams)
      Returns a stream that concatenates the given streams.
      Since:
      3.26