Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Get the stream from Enumeration in Java

In this post, I am going to explain how to get the Stream from an Enumeration in Java.

 

Step 1: Get an instance of Spliterator from Enumeration.

Spliterator spliterator = new Spliterators.AbstractSpliterator(Long.MAX_VALUE, Spliterator.ORDERED) {

	@Override
	public void forEachRemaining(Consumer super T> consumer) {
		while (enumeration.hasMoreElements()) {
			consumer.accept(enumeration.nextElement());
		}
	}

	@Override
	public boolean tryAdvance(Consumer super T> consumer) {
		if (enumeration.hasMoreElements()) {
			consumer.accept(enumeration.nextElement());
			return true;
		}
		return false;
	}

};

 

As you see above snippet, I defined the Spliterator instance by overriding forEachRemaining and tryAdvance methods.

 

forEachRemaining’ method perform the given action for each remaining element, sequentially in the current thread, until all elements have been processed or the action throws an exception.

 

tryAdvance’ method performs action on any remaining element in the spliterator and return true if the element exists, else false.

 

Step 2: Use StreamSupport.stream method to get the stream from a spliterator.

public static Stream stream(Spliterator spliterator, boolean parallel)

Creates a new sequential or parallel Stream from a Spliterator. If the argument 'parallel' is set to true then the returned stream is a parallel stream. If the argument 'parallel' is set to false then the returned stream is a sequential stream.

 

Example

StreamSupport.stream(spliterator, false);

 

Find the below working application.


StreamFromEnumeration.java

package com.sample.app.streams;

import java.util.Enumeration;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.StringTokenizer;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class StreamFromEnumeration {
	public static StreamtoStream(final Enumeration enumeration) {
		Spliterator spliterator = new Spliterators.AbstractSpliterator(Long.MAX_VALUE, Spliterator.ORDERED) {

			@Override
			public void forEachRemaining(Consumer super T> consumer) {
				while (enumeration.hasMoreElements()) {
					consumer.accept(enumeration.nextElement());
				}
			}

			@Override
			public boolean tryAdvance(Consumer super T> consumer) {
				if (enumeration.hasMoreElements()) {
					consumer.accept(enumeration.nextElement());
					return true;
				}
				return false;
			}

		};

		return StreamSupport.stream(spliterator, false);
	}

	public static void main(String[] args) {
		String str = "Hello there, How are you!!!!";

		StringTokenizer tokenizer = new StringTokenizer(str, " ");

		toStream(tokenizer).forEach(System.out::println);
	}

}

 

Output

Hello
there,
How
are
you!!!!

 

 

You may like

Interview Questions

Collection programs in Java

Array programs in Java

Get the last element from a collection in Java

Implement stack data structure using List

Get a composite unmodifiable array list from two arrays

Join the collection elements by a separator using streams

Get the stream from an iterator in Java



This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Get the stream from Enumeration in Java

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×