Java Program to Print a HashSet Collection Using the Foreach Loop

You are currently viewing Java Program to Print a HashSet Collection Using the Foreach Loop

Java program to print a HashSet collection using the foreach loop

// Java program to print a HashSet collection 
// using the foreach loop

import java.util.*;

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

    nums.add("One");
    nums.add(2);
    nums.add(3.14);
    nums.add(true);

    System.out.println("Set items: ");
    for (Object item: nums)
      System.out.println(" " + item);
  }
}

Output

Set items: 
 2
 3.14
 One
 true

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