Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java Enumeration to List

farenda 2015-07-24 0

Problem:

When using legacy libraries sometimes you need to convert Java Enumeration to List to use it with modern code it comfortable way. Again Java Collections API comes to the rescue!

Solution:

The following Java example shows how to use legacy Enumeration in modern Java code. Also note how the content disappears from enumeration:

package com.farenda.java;

import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

public class CollectionsListEnumeration {

    public static void main(String[] args) {
        List<String> items = Arrays.asList("evaporating", "content");

        Enumeration<String> en = Collections.enumeration(items);
        print(en);

        // Legacy Enumeration to List:
        List<String> list = Collections.list(en);
        System.out.println("As list (content is gone!): " + list);

        // Again, but without iterating:
        en = Collections.enumeration(items);
        list = Collections.list(en);
        System.out.println("\nAs list: " + list);
        print(en);
    }

    private static void print(Enumeration<String> en) {
        System.out.println("Enumeration: ");
        while (en.hasMoreElements()) {
            System.out.println(en.nextElement());
        }
    }
}

Here, the key methods is java.util.Collections.list(Enumeration) that turns legacy Java Enumeration into List. The code is simple, but note that content of enumeration once used is gone!

The output of running the example illustrates that:

Enumeration:
evaporating
content
As list (content is gone!): []

As list: [evaporating, content]
Enumeration:

Printing the content of Enumeration makes it empty. Also creating a List from Enumeration makes the content disappear, which means that Enumeration cannot be reused.

Share with the World!
Categories Java Tags java-collections
Previous: Java List sublist
Next: Clojure switch case

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored