Class StreamUtil

java.lang.Object
uk.ac.starlink.util.StreamUtil

public class StreamUtil extends Object
Utilities for use with java Streams.
Since:
23 Jun 2023
Author:
Mark Taylor
  • Method Details

    • keepInstances

      public static <T, R> Function<T,Stream<R>> keepInstances(Class<R> keepClazz)
      Utility function that can be used to filter streams to exclude any elements that are not instances of a particular type. This doesn't do anything particularly complicated, but it allows one to combine a filter (for class) step with a type-casting step in a way which is commonly required and otherwise annoyingly verbose.

      Use the result of this method as the argument to Stream.flatMap(java.util.function.Function<? super T, ? extends java.util.stream.Stream<? extends R>>).

      Parameters:
      keepClazz - class for which instances are required
      Returns:
      function for use in flatMap