Java Program to Convert a HashSet Into an Array

You are currently viewing Java Program to Convert a HashSet Into an Array

Java program to convert a HashSet into an array

// Java program to convert a HashSet 
// into an array

import java.util.*;

public class Main {
  public static void main(String[] args) {
    HashSet < Integer > nums = new HashSet();

    nums.add(1);
    nums.add(2);
    nums.add(3);
    nums.add(4);
    nums.add(5);
    nums.add(6);

    Object arr[] = nums.toArray();

    System.out.println("Array elements: ");
    for (Object item: arr)
      System.out.print(item + " ");
  }
}

Output

Array elements: 
1 2 3 4 5 6

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply