Java, Software Development

Java 8 – Convert Iterator to Stream

Although not a common scenario, some libraries just return Iterator instead of actual Stream or Collection objects.

Iterator only

Consider these codes. Imagine they belong to some third-party library.

As you can see, to get the contents of someData you need to go through an Iterator.


Java 8 has StreamSupport to allow us to create a Stream object.

The output for these codes:

You Might Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *