This example shows how to print HashSet in Java. This example also shows how to print HashSet elements using for loop, Arrays class, and Java stream.
How to print HashSet elements in Java?
There are several ways using which we can print HashSet elements or display HashSet elements in the console as given below.
1. Using the for loop
We can use the enhanced for loop to iterate over HashSet object and display the elements one by one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.HashSet; import java.util.Set; public class JavaPrintHashSetExample { public static void main(String[] args) { Set<String> hSetOddNumbers = new HashSet<String>(); hSetOddNumbers.add("One"); hSetOddNumbers.add("Three"); hSetOddNumbers.add("Five"); //using enhanced for loop for( String strCurrentNumber : hSetOddNumbers ){ System.out.println( strCurrentNumber ); } } } |
Output
1 2 3 |
Five One Three |
2. Using the Iterator
Instead of using the for loop, we can also use the Iterator as given below.
1 2 3 4 5 6 7 8 9 10 |
Set<String> hSetOddNumbers = new HashSet<String>(); hSetOddNumbers.add("One"); hSetOddNumbers.add("Three"); hSetOddNumbers.add("Five"); Iterator<String> itr = hSetOddNumbers.iterator(); while(itr.hasNext()){ System.out.println( itr.next() ); } |
Output
1 2 3 |
Five One Three |
3. Using the Arrays class
We can convert the HashSet to an array and then use the toString
method of the Arrays class to display the array as given below.
1 2 3 4 5 6 7 |
Set<String> hSetOddNumbers = new HashSet<String>(); hSetOddNumbers.add("One"); hSetOddNumbers.add("Three"); hSetOddNumbers.add("Five"); System.out.println( Arrays.toString( hSetOddNumbers.toArray() ) ); |
Output
1 |
[Five, One, Three] |
If you do not want the opening and closing square brackets, you can remove them by using the regular expression along with the String replaceAll method as given below.
1 2 3 4 5 6 7 8 9 10 |
Set<String> hSetOddNumbers = new HashSet<String>(); hSetOddNumbers.add("One"); hSetOddNumbers.add("Three"); hSetOddNumbers.add("Five"); System.out.println( Arrays .toString(hSetOddNumbers.toArray()) .replaceAll("[\\[\\]]", "") ); |
Output
1 |
Five, One, Three |
4. Using forEach (Java 8)
If you are using Java version 8 or later, you can use the forEach
method to print the HashSet elements.
1 2 3 4 5 6 7 8 9 |
Set<String> hSetOddNumbers = new HashSet<String>(); hSetOddNumbers.add("One"); hSetOddNumbers.add("Three"); hSetOddNumbers.add("Five"); hSetOddNumbers.forEach( element ->{ System.out.println(element); }); |
Output
1 2 3 |
Five One Three |
How to print HashSet of custom class objects?
Let’s see what happens when we try to print HashSet of custom class objects using the enhanced for loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.HashSet; import java.util.Set; class Order{ private int orderNumber; public Order(int orderNumber){ this.orderNumber = orderNumber; } } public class JavaPrintHashSetExample { public static void main(String[] args) { Set<Order> hSetOrders = new HashSet<Order>(); hSetOrders.add(new Order(1001)); hSetOrders.add(new Order(1002)); for(Order order : hSetOrders){ System.out.println(order); } } } |
Output
1 2 |
com.javacodeexamples.collections.hashset.Order@7852e922 com.javacodeexamples.collections.hashset.Order@6d06d69c |
Since the Order custom class has not overridden the toString
method, the toString
method inherited from the Object class is used when we print the element. The toString
method of the Object class returns a string containing the object information in “class_name@object_hashcode” format that is not useful to read.
If we want to print useful information, we need to override the toString
method in the Order class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.HashSet; import java.util.Set; class Order{ private int orderNumber; public Order(int orderNumber){ this.orderNumber = orderNumber; } public String toString(){ return "Order: " + this.orderNumber; } } public class JavaPrintHashSetExample { public static void main(String[] args) { Set<Order> hSetOrders = new HashSet<Order>(); hSetOrders.add(new Order(1001)); hSetOrders.add(new Order(1002)); for(Order order : hSetOrders){ System.out.println(order); } } } |
Output
1 2 |
Order: 1002 Order: 1001 |
This example is a part of the HashSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 HashSet