How do I turn a Java Enumeration into a Stream?

前端 未结 5 2151
攒了一身酷
攒了一身酷 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:43

    According to Guava docs, you could use the Iterators.forEnumeration() method:

    Enumeration enumeration = ...;
    
    Iterator iterator = Iterators.forEnumeration(enumeration);
    

    And in this question, it is explained how to get a stream from an iterator:

    Stream stream = StreamSupport.stream(
        Spliterators.spliteratorUnknownSize(
            iterator, Spliterator.ORDERED),
        false);
    

提交回复
热议问题