uk.ac.starlink.util

## Class StreamUtil

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

-     public class StreamUtil
      extends java.lang.Object

  Utilities for use with java Streams.
  Since:
    23 Jun 2023

- - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static <T,R> java.util.function.Function<T,java.util.stream.Stream<R>>` | `keepInstances(java.lang.Class<R> keepClazz)` Utility function that can be used to filter streams to exclude any elements that are not instances of a particular type.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### keepInstances

          public static <T,R> java.util.function.Function<T,java.util.stream.Stream<R>> keepInstances(java.lang.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