Last active
June 5, 2018 04:13
-
-
Save mjstrasser/57ad0e489bf36db88072c9f53f535c44 to your computer and use it in GitHub Desktop.
Utilities for working with Java streams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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