This example shows how to print LinkedHashSet elements (display LinkedHashSet) in Java. This example also shows how to print LinkedHashSet in the console using for loop, Arrays class, forEach, and Java Stream.
How to print LinkedHashSet elements in Java?
There are several ways using which we can print LinkedHashSet elements in the console or a log file as given below.
1. Using the enhanced for loop
We can use the enhanced for loop to iterate LinkedHashSet elements and print them.
1 2 3 4 5 6 7 8 9 10 |
LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); lhSetColors.add("red"); lhSetColors.add("green"); lhSetColors.add("blue"); //print using for loop for(String strColor : lhSetColors){ System.out.println(strColor); } |
Output
1 2 3 |
red green blue |
2. Using the Iterator
We can get an iterator over LinkedHashSet elements using the iterator
method and then iterate and print the elements using the hasNext
and next
methods as given below.
1 2 3 4 5 6 7 8 9 10 11 12 |
LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); lhSetColors.add("red"); lhSetColors.add("green"); lhSetColors.add("blue"); //print using Iterator Iterator<String> itr = lhSetColors.iterator(); while(itr.hasNext()){ System.out.println( itr.next() ); } |
Output
1 2 3 |
red green blue |
3. Using the forEach method
If you are using Java version 8 or later, you can also use the forEach
method to print the elements.
1 2 3 4 5 6 7 8 9 |
LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); lhSetColors.add("red"); lhSetColors.add("green"); lhSetColors.add("blue"); lhSetColors.forEach( element ->{ System.out.println( element ); }); |
Output
1 2 3 |
red green blue |
4. Using the toString method of the Arrays class
In order to use the toString
method of the Arrays class, we first need to convert LinkedHashSet to an array. Once converted, the toString
method can be used to display the LinkedHashSet elements.
1 2 3 4 5 6 7 |
LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); lhSetColors.add("red"); lhSetColors.add("green"); lhSetColors.add("blue"); System.out.println( Arrays.toString( lhSetColors.toArray()) ); |
Output
1 |
[red, green, blue] |
If you want to remove the square brackets and spaces from between the elements, you can use the “[\\[\\]\\s]” regular expression pattern along with the String replaceAll method.
1 2 3 4 5 6 7 |
LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); lhSetColors.add("red"); lhSetColors.add("green"); lhSetColors.add("blue"); System.out.println( Arrays.toString( lhSetColors.toArray()).replaceAll("[\\[\\]\\s]", "") ); |
Output
1 |
red,green,blue |
How to print LinkedHashSet having objects of a custom class?
When any object is printed, its toString
method is called to get the string representation. If the custom class has not overridden it, the toString
method inherited from the Object class is called.
The toString
method of the Object class prints the object information in “ClassName@ObjectHashCode” format.
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 |
import java.util.LinkedHashSet; class Student{ private Integer id; private String name; public Student(Integer id, String name){ this.id = id; this.name = name; } } public class PrintLinkedHashSetExample { public static void main(String[] args) { LinkedHashSet<Student> lhSetStudents = new LinkedHashSet<Student>(); lhSetStudents.add(new Student(1, "John")); lhSetStudents.add(new Student(2, "Maria")); System.out.println(lhSetStudents); } } |
Output
1 |
[com.javacodeexamples.collections.linkedhashset.Student@6d06d69c, com.javacodeexamples.collections.linkedhashset.Student@7852e922] |
As we can see from the output, the object information is not very useful. We can solve this by implementing the toString
method in the Student custom 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 |
class Student{ private Integer id; private String name; public Student(Integer id, String name){ this.id = id; this.name = name; } public String toString(){ return "[" + this.id + "=>" + this.name + "]"; } } public class PrintLinkedHashSetExample { public static void main(String[] args) { LinkedHashSet<Student> lhSetStudents = new LinkedHashSet<Student>(); lhSetStudents.add(new Student(1, "John")); lhSetStudents.add(new Student(2, "Maria")); System.out.println(lhSetStudents); } } |
Output
1 |
[[1=>John], [2=>Maria]] |
This example is a part of the Java LinkedHashSet Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashSet