Open In App

How to Print LinkedHashSet Elements in Java?

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:



  1. By simply printing the elements
  2. By using enhanced for loop
  3. Using iterator
  4. Using Arrays.toString()
  5. Printing elements of LinkedHashSet having objects of custom class using .toString() method of Object Class.

Method 1: Simply Printing the elements to console




// Java program to print the elements of LinkedHashSet
 
import java.util.*;
class GfG {
    public static void main(String args[])
    {
 
        // Creating an instance of LinkedHashSet
        LinkedHashSet<String> hs = new LinkedHashSet<>();
 
        // Adding elements to the LinkedHashSet
        hs.add("Hey");
        hs.add("How");
        hs.add("are");
        hs.add("You");
 
        // Printing
        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:-




// Java program to print the elements of LinkedHashSet
 
import java.util.*;
class GfG {
    public static void main(String[] args)
    {
        // Creating an instance of LinkedHashSet
        LinkedHashSet<Integer> h = new LinkedHashSet<Integer>();
 
        // Adding elements to the LinkedHashSet
        h.add(1);
        h.add(5);
        h.add(3);
        h.add(9);
 
        // iterating and printing all the elements of
        // LinkedHashSet
        for (int i : h)
        {
            System.out.print(i + " ");
        }
    }
}

Output
1 5 3 9

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:-




// Java program to print the elements of LinkedHashSet
 
import java.util.*;
 
class GfG {
    public static void main(String[] args)
    {
 
        // Creating an instance of LinkedHashSet
        LinkedHashSet<String> hnames = new LinkedHashSet<String>();
 
        // Adding elements to the LinkedHashSet
        hnames.add("Alok");
        hnames.add("Ayush");
        hnames.add("Abhinav");
        hnames.add("Akash");
        hnames.add("Atul");
 
        // Obtaining an Iterator
        Iterator<String> itr = hnames.iterator();
 
        // printing the elements in LinkedHashSet
        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.




// Java program to print the elements of LinkedHashSet
 
import java.util.*;
 
class GfG {
    public static void main(String[] args)
    {
 
        // Creating an instance of LinkedHashSet
        LinkedHashSet<Integer> hnum
            = new LinkedHashSet<Integer>();
 
        // Adding elements to the LinkedHashSet
        hnum.add(678);
        hnum.add(789);
        hnum.add(876);
        hnum.add(589);
 
        // Converting the LinkedHashSet to Array
        // and then converting the array to string
        System.out.println(Arrays.toString(hnum.toArray()));
    }
}

Output
[678, 789, 876, 589]

Method 5: Printing elements of LinkedHashSet having objects of custom class 




// Java program to print the elements of LinkedHashSet
 
import java.util.*;
class employee {
    private Integer salary;
    private String name;
 
    public employee(Integer salary, String name)
    {
        this.salary = salary;
        this.name = name;
    }
     
    // This method is of Object Class
    // and whenever the object of employee
    // class would be made then this method
    // will be called by default
    public String toString()
    {
        return "[" + this.name + "=>" + this.salary + "]";
    }
}
 
class GfG {
    public static void main(String[] args)
    {
 
        // Creating an instance of LinkedHashSet
        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]]

Article Tags :