Skip to content

Instantly share code, notes, and snippets.

@mjstrasser
Last active June 5, 2018 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjstrasser/57ad0e489bf36db88072c9f53f535c44 to your computer and use it in GitHub Desktop.
Save mjstrasser/57ad0e489bf36db88072c9f53f535c44 to your computer and use it in GitHub Desktop.
Utilities for working with Java streams
package mjs.stream.utils;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class StreamUtils {
/**
* Converts a stream into an iterable for use by, e.g. Spring CrudRepository classes.
*/
public static <T> Iterable<T> asIterable(Stream<T> stream) {
return stream::iterator;
}
/**
* Converts an iterable into a sequential stream.
*/
public static <T> Stream<T> asStream(Iterable<T> iterable) {
return StreamSupport.stream(iterable.spliterator(), false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment