Enumeration Iteration in Java 8

Is it possible to repeat the iteration of Enumeration with a Lambda expression? What will be the representation of the Lambda of the following code fragment:

 Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); while (nets.hasMoreElements()) { NetworkInterface networkInterface = nets.nextElement(); } 

I did not find a stream in it.

+70
java lambda iteration enumeration java-8
Apr 24 '14 at 6:51
source share
7 answers

If you donโ€™t like the fact that Collections.list(Enumeration) copies all the contents to a temporary list before iteration, you can help yourself in a simple way:

 public static <T> void forEachRemaining(Enumeration<T> e, Consumer<? super T> c) { while(e.hasMoreElements()) c.accept(e.nextElement()); } 

Then you can just do forEachRemaining(enumeration, lambda-expression); (recall the import static function) ...

+33
Apr 24 '14 at 13:32
source share

(This answer shows one of many options. The fact that he has a mark of acceptance does not mean that he is the best. I suggest reading the other answers and choosing them depending on the situation in which you are. IMO:
- For Java 8, Holger will answer better, because, in addition to simplicity, it does not require additional iteration, which happens in my solution.
- for Java 9 I would choose a solution from Tagir Valeevโ€™s answer )




You can copy the elements from your Enumeration into an ArrayList with Collections.list and then use it as

 Collections.list(yourEnumeration).forEach(yourAction); 
+84
Apr 24 '14 at 6:59
source share

If your code has a lot of enumerations, I recommend creating a static helper method that converts Enumeration to Stream. A static method might look like this:

 public static <T> Stream<T> enumerationAsStream(Enumeration<T> e) { return StreamSupport.stream( Spliterators.spliteratorUnknownSize( new Iterator<T>() { public T next() { return e.nextElement(); } public boolean hasNext() { return e.hasMoreElements(); } }, Spliterator.ORDERED), false); } 

Use a method with static imports. Unlike the Holger solution, you can take advantage of various stream operations that can make existing code even simpler. Here is an example:

 Map<...> map = enumerationAsStream(enumeration) .filter(Objects::nonNull) .collect(groupingBy(...)); 
+50
Apr 24 '14 at 18:01
source share

Since Java-9 will be the new default method Enumeration.asIterator() , which will simplify a clean Java solution:

 nets.asIterator().forEachRemaining(iface -> { ... }); 
+45
Sep 17 '15 at 10:21
source share

You can use the following combination of standard functions:

 StreamSupport.stream(Spliterators.spliteratorUnknownSize(CollectionUtils.toIterator(enumeration), Spliterator.IMMUTABLE), parallel) 

You can also add additional features such as NONNULL or DISTINCT .

After applying static imports, this will become more readable:

 stream(spliteratorUnknownSize(toIterator(enumeration), IMMUTABLE), false) 

you now have a standard Java 8 thread that you can use in any way! You can pass true for parallel processing.

To convert from Enumeration to Iterator, use any of:

  • CollectionUtils.toIterator() from Spring 3.2 or you can use
  • IteratorUtils.asIterator() from the Apache Commons 3.2 collection
  • Iterators.forEnumeration() by Google Guava
+11
Nov 26 '14 at 8:53
source share

For Java 8, the simplest conversion of an enum to stream is:

Collections.list(NetworkInterface.getNetworkInterfaces()).stream()

+5
Jan 29 '18 at 14:45
source share

I know this is an old question, but I wanted to introduce an alternative to the functionality of Collections.asList and Stream. Since the question is entitled โ€œIteration of an Enumeration,โ€ I admit that sometimes you want to use a lambda expression, but an extended loop may be preferable because the enumerated object can throw an exception, and the for loop is easier to encapsulate in a larger attempt. catch code segment (lambdas require declared exceptions to be caught in lambdas). To do this, we use a lambda expression to create an Iterable, which you can use in a for loop and not preload the enumeration:

  /** * Creates lazy Iterable for Enumeration * * @param <T> Class being iterated * @param e Enumeration as base for Iterator * @return Iterable wrapping Enumeration */ public static <T> Iterable<T> enumerationIterable(Enumeration<T> e) { return () -> new Iterator<T>() { @Override public T next() { return e.nextElement(); } @Override public boolean hasNext() { return e.hasMoreElements(); } }; } 
+4
Apr 19 '18 at 14:52
source share



All Articles