How to Find User Defined Objects From LinkedHashSet in Java?
Last Updated :
28 Jun, 2021
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.");
}
}
}
OutputDheeraj 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.");
}
}
}
}
OutputBina present in set.
Similar Reads
How to Delete User Defined Objects from LinkedHashSet? The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. 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
2 min read
How to Eliminate Duplicate User Defined Objects from LinkedHashSet in Java? While creating a HashSet of your own class, always ensure that the HashCode() method of the key of HashSet doesnât change. Java Object hashCode() is a native method and returns the integer hash code value of the object. If two objects are equal according to the equals() method, then their hash code
3 min read
How to Eliminate Duplicate User Defined Objects as a Key from Java LinkedHashMap? Duplicate user-defined objects as a key from Java LinkedHashMap can be removed and achieved by implementing equals and hashcode methods at the user-defined objects. Example: Input : LinkedHashMap = [{[Apple, 40], Kashmir}, {[Grapes, 80], Nashik}] Duplicate key = {[Grapes, 80], Delhi} Output: LinkedH
2 min read
How to Get Random Elements from LinkedHashSet in Java? LinkedHashSet is used to maintain the insertion order and for generating random elements from LinkedHashSet we will use Random Class to generate a random number between 0 and the LinkedHashSet size. That random number will act as the index of LinkedHashSet. We can get a random element in three ways:
3 min read
How to Get the Last Element from LinkedHashSet in Java? The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. 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
2 min read
How to Find the Minimum or Maximum Element from LinkedHashSet in Java? Given a LinkedHashSet the task is to find the minimum and maximum element from this LinkedHashSet in Java. There are two ways to find the minimum or maximum element from LinkedHashSet in Java as follows Using the min and max method of the Collections classBy Iterating the LinkedHashSetExamples: Inpu
2 min read