Wednesday 3 May 2023

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<T> spliterator = new Spliterators.AbstractSpliterator<T>(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 <T> Stream<T> stream(Spliterator<T> 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 <T> Stream<T> toStream(final Enumeration<T> enumeration) {
		Spliterator<T> spliterator = new Spliterators.AbstractSpliterator<T>(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

No comments:

Post a Comment