Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaHow to Find User Defined Objects From LinkedHashSet in Java?

How to Find User Defined Objects From LinkedHashSet in Java?

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
  
import java.util.*;
  
// Student class
class Student {
  
    int rollNo;
    String name;
  
    Student(int r, String s)
    {
        rollNo = r;
        name = s;
    }
}
  
public class GFG {
    public static void main(String[] args)
    {
        // Objects of Student
        Student stu1 = new Student(1, "Akshay");
        Student stu2 = new Student(2, "Bina");
        Student stu3 = new Student(3, "Chintu");
        Student stu4 = new Student(4, "Dheeraj");
  
        // New empty LinkedHashSet of type Student
        LinkedHashSet<Student> set = new LinkedHashSet<>();
  
        // 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.");
        }
    }
}


Output

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 
  
import java.util.*;
  
// Student class
class Student {
  
    int rollNo;
    String name;
  
    Student(int r, String s)
    {
        rollNo = r;
        name = s;
    }
}
  
public class GFG {
    public static void main(String[] args)
    {
        // Objects of Student
        Student stu1 = new Student(1, "Akshay");
        Student stu2 = new Student(2, "Bina");
        Student stu3 = new Student(3, "Chintu");
        Student stu4 = new Student(4, "Dheeraj");
  
        // New empty LinkedHashSet of type Student
        LinkedHashSet<Student> set = new LinkedHashSet<>();
  
        // 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.");
            }
        }
    }
}


Output

Bina present in set.
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments