Java Program to convert HashSet to Enumeration

Java 8Object Oriented ProgrammingProgramming

<p>Let&rsquo;s say the following is our HashSet:</p><pre class="result notranslate">HashSet&lt;String&gt; set = new HashSet&lt;String&gt;(); set.add(&quot;P&quot;); set.add(&quot;Q&quot;); set.add(&quot;R&quot;); set.add(&quot;S&quot;); set.add(&quot;T&quot;); set.add(&quot;U&quot;); set.add(&quot;V&quot;); set.add(&quot;W&quot;); set.add(&quot;X&quot;); set.add(&quot;Z&quot;);</pre><p>Now convert the above HashSet to Enumeration:</p><pre class="result notranslate">Enumeration&lt;String&gt; enumeration = Collections.enumeration(set); while (enumeration.hasMoreElements()) &nbsp; &nbsp;System.out.println(enumeration.nextElement());</pre><h2>Example</h2><pre class="prettyprint notranslate">import java.util.Collections; import java.util.Enumeration; import java.util.HashSet; public class Demo { &nbsp; &nbsp;public static void main(String[] args) { &nbsp; &nbsp; &nbsp; HashSet&lt;String&gt; set = new HashSet&lt;String&gt;(); &nbsp; &nbsp; &nbsp; set.add(&quot;P&quot;); &nbsp; &nbsp; &nbsp; set.add(&quot;Q&quot;); &nbsp; &nbsp; &nbsp; set.add(&quot;R&quot;); &nbsp; &nbsp; &nbsp; set.add(&quot;S&quot;); &nbsp; &nbsp; &nbsp; set.add(&quot;T&quot;); &nbsp; &nbsp; &nbsp; set.add(&quot;U&quot;); &nbsp; &nbsp; &nbsp; set.add(&quot;V&quot;); &nbsp; &nbsp; &nbsp; set.add(&quot;W&quot;); &nbsp; &nbsp; &nbsp; set.add(&quot;X&quot;); &nbsp; &nbsp; &nbsp; set.add(&quot;Z&quot;); &nbsp; &nbsp; &nbsp; Enumeration&lt;String&gt; enumeration = Collections.enumeration(set); &nbsp; &nbsp; &nbsp; while (enumeration.hasMoreElements()) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(enumeration.nextElement()); &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">P Q R S T U V W X Z</pre>
raja
Updated on 30-Jul-2019 22:30:25

Advertisements