I have a third party library that gives me an Enumeration<String>
. I want to work with that enumeration lazily as a Java 8 Stream
, calling things like filter
, map
and flatMap
on it.
Is there an existing library that has this in it? I am already referencing Guava and Apache Commons so if either of those have the solution that would be ideal.
Alternatively, what is the best/easiest way to turn an Enumeration
into a Stream
while retaining the lazy nature of everything?
Why not using vanilla Java :
Collections.list(enumeration).stream()...
However as mentionned by @MicahZoltu, the number of items in the enumeration has to be taken into account, as Collections.list
will first iterate over the enumeration to copy the elements in an ArrayList
. From there the regular stream
method can be used. While this is usual for many collection stream operations, if the enumeration is too big (like infinite), this can cause problem because the enumeration has to be transformed in a list then the other approaches described here should be used instead.