How to Print LinkedHashSet Elements in Java?
Last Updated :
17 Nov, 2022
LinkedHashSet is a child class of HashSet in which duplicates are not allowed but the insertion order is preserved. The elements are printed in the same order in which they were inserted.
There are several ways to print LinkedHashSet elements:
- By simply printing the elements
- By using enhanced for loop
- Using iterator
- Using Arrays.toString()
- Printing elements of LinkedHashSet having objects of custom class using .toString() method of Object Class.
Method 1: Simply Printing the elements to console
- We’ll first make an instance of the LinkedHashSet ,add the elements in it and simply print it to the console.
- One method is to simply print the Collection object name and the contents of the LinkedHashSet will be printed.
Java
import java.util.*;
class GfG {
public static void main(String args[])
{
LinkedHashSet<String> hs = new LinkedHashSet<>();
hs.add( "Hey" );
hs.add( "How" );
hs.add( "are" );
hs.add( "You" );
System.out.println(hs);
}
}
|
Output
[Hey, How, are, You]
Method 2: Using Enhanced for loop
We can simply print the elements using the enhanced for loop concept or for-each loop concept by iterating over all the LinkedHashSet elements.
To print the elements using for-each loop follow the following steps:-
- Set up a for-each loop by declaring a variable of the same type as the base type of Collection class i.e. of the same type as the elements of LinkedHashSet, followed by a colon, which is then followed by the object name.
- Inside the loop, simply print the variable name to display all the contents.
Java
import java.util.*;
class GfG {
public static void main(String[] args)
{
LinkedHashSet<Integer> h = new LinkedHashSet<Integer>();
h.add( 1 );
h.add( 5 );
h.add( 3 );
h.add( 9 );
for ( int i : h)
{
System.out.print(i + " " );
}
}
}
|
Method 3: By using an Iterator
We can use an iterator over LinkedHashSet elements using the iterator method and print them using next() and hasNext() methods.
To cycle through the contents of the LinkedhashSet following steps will be followed:-
- Obtain an iterator to start by calling an Iterator method.
- Set up a loop that calls hasNext() and iterate until hasNext() return true.
- Inside the loop, print each element using next().
Java
import java.util.*;
class GfG {
public static void main(String[] args)
{
LinkedHashSet<String> hnames = new LinkedHashSet<String>();
hnames.add( "Alok" );
hnames.add( "Ayush" );
hnames.add( "Abhinav" );
hnames.add( "Akash" );
hnames.add( "Atul" );
Iterator<String> itr = hnames.iterator();
while (itr.hasNext())
{
System.out.println(itr.next());
}
}
}
|
Output
Alok
Ayush
Abhinav
Akash
Atul
Method 4: Using Array.toString()
To use it we first need to convert the LinkedHashSet to an array and then use the .toString() method to print them.
- Convert the contents of the LinkedHashSet to an Array using .toArray method.
- Now convert the arrays elements to their String representation using Arrays.toString() and print them.
Java
import java.util.*;
class GfG {
public static void main(String[] args)
{
LinkedHashSet<Integer> hnum
= new LinkedHashSet<Integer>();
hnum.add( 678 );
hnum.add( 789 );
hnum.add( 876 );
hnum.add( 589 );
System.out.println(Arrays.toString(hnum.toArray()));
}
}
|
Output
[678, 789, 876, 589]
Method 5: Printing elements of LinkedHashSet having objects of custom class
- We can print the contents of the custom class simply by the below approach but there is a key thing that needs to be kept in mind before printing it.
- Whenever an object is printed its .toString() method is called to get its String representation, if the custom class has not overridden the method then it is inherited from the Object class.
- The toString() of the Object class prints the contents of the object as “ClassName@ObjectHashCode”, this would not be our requirement, so we override the toString() in the custom class so that only the information that we require will be printed.
Java
import java.util.*;
class employee {
private Integer salary;
private String name;
public employee(Integer salary, String name)
{
this .salary = salary;
this .name = name;
}
public String toString()
{
return "[" + this .name + "=>" + this .salary + "]" ;
}
}
class GfG {
public static void main(String[] args)
{
LinkedHashSet<employee> hemp = new LinkedHashSet<employee>();
hemp.add( new employee( 100000 , "Ankush" ));
hemp.add( new employee( 200000 , "Atul" ));
System.out.println(hemp);
}
}
|
Output
[[Ankush=>100000], [Atul=>200000]]
Share your thoughts in the comments
Please Login to comment...