How do I turn a Java Enumeration into a Stream?

前端 未结 5 2190
攒了一身酷
攒了一身酷 2021-02-01 00:11

I have a third party library that gives me an Enumeration. I want to work with that enumeration lazily as a Java 8 Stream, calling thing

5条回答
  •  情深已故
    2021-02-01 00:40

    In my StreamEx library there's simple method StreamEx.of(Enumeration) which does the job:

    Stream stream = StreamEx.of(enumeration);
    

    Note that it not just a shortcut to the @Holger solution, but implemented in different manner. In particular, it has significantly better parallel execution characteristics compared to solutions involving Spliterators.spliteratorUnknownSize().

提交回复
热议问题