LinkedHashSet is used to store elements in which order they were inserted. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were inserted. When cycling through LinkedHashSet using an iterator, the elements will be returned to the order in which they were inserted.
We have a class Student and a LinkedHashSet of type Student, and we want to see that the LinkedHashSet contains the object or not.
// Student class
class Student {
  int rollNo;
  String name;
  Student(int r, String s) {
    rollNo = r;
    name = s;
  }
}
// LinkedHashSet of type Student
LinkedHashSet<Student> set = new LinkedHashSet<>();
There are two ways to find the object in the LinkedHashSet:
- Using contains() method
- Using iterate over LinkedHashSet
Method 1: Using contains() method
We can find the object in the LinkedHashSet using the contains() method in Java. The contains() method in Java returns true if the object present in LinkedHashSet otherwise, it returns false.
Java
| // Java Program to find user defined objects from// LinkedHashSet Âimportjava.util.*; Â// Student classclassStudent { Â    introllNo;    String name; Â    Student(intr, String s)    {        rollNo = r;        name = s;    }} ÂpublicclassGFG {    publicstaticvoidmain(String[] args)    {        // Objects of Student        Student stu1 = newStudent(1, "Akshay");        Student stu2 = newStudent(2, "Bina");        Student stu3 = newStudent(3, "Chintu");        Student stu4 = newStudent(4, "Dheeraj"); Â        // New empty LinkedHashSet of type Student        LinkedHashSet<Student> set = newLinkedHashSet<>(); Â        // Add objects to set        set.add(stu1);        set.add(stu2);        set.add(stu3); Â        // Return true if set contains the obj otherwise,        // false        if(set.contains(stu4))        {            System.out.println(stu4.name+ " is present in set.");        }        else        {            System.out.println(stu4.name+ " is not present in set.");        }    }} | 
Dheeraj is not present in set.
Method 2: Using iterate over LinkedHashSet:
We can also find the object in LinkedHashSet using iterate over it.
Java
| // Java Program to find user defined objects from// LinkedHashSet  Âimportjava.util.*; Â// Student classclassStudent { Â    introllNo;    String name; Â    Student(intr, String s)    {        rollNo = r;        name = s;    }} ÂpublicclassGFG {    publicstaticvoidmain(String[] args)    {        // Objects of Student        Student stu1 = newStudent(1, "Akshay");        Student stu2 = newStudent(2, "Bina");        Student stu3 = newStudent(3, "Chintu");        Student stu4 = newStudent(4, "Dheeraj"); Â        // New empty LinkedHashSet of type Student        LinkedHashSet<Student> set = newLinkedHashSet<>(); Â        // Add objects to set        set.add(stu1);        set.add(stu2);        set.add(stu3); Â        // Using iterate LinkedHashSet        for(Student stu : set)         {            if(stu == stu2)            {                System.out.println(stu.name+ " present in set.");            }        }    }} | 
Bina present in set.

 
                                    







