How to Create a LinkedHashSet with Custom Equality for Objects? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we want to create a LinkedHashSet of custom objects with specific equality criteria. So, we can create it by overriding the equals and hashcode methods in Java. In the equals() method, we can put the custom criteria over there and we override the hashcode to provide the same Hashcode to our custom object. Example:We want to implement a program that stores the information of students along with its insertion order. However the values must be unique, and the students cannot use the same identities. Approach:In this example, first we create the custom object class Student. Then we will create a constructor where we accept two values StudenentID and StudentName.And then override the equals() method with the custom criteria.After that, we Override the hashCode method so the Student type object has the same Hashcode.We will create a get() method for name and ID.After that, we create the LinkedHashSet of the Student type and then add value to it.Program to Create a LinkedHashSet of Custom Objects with Specific Equality CriteriaBelow is the code implementation of the creation of a LinkedHashSet of custom objects with specific equality criteria. Java // Java Program to Create a LinkedHashSet of // Custom Objects with Specific Equality Criteria import java.io.*; import java.util.LinkedHashSet; import java.util.Objects; // Driver Class class GFG { // Main Function public static void main(String[] args) { // Create a LinkedHashSet LinkedHashSet<Student> StudentSet = new LinkedHashSet<>(); // Add Students in the Set StudentSet.add(new Student(1, "Amit")); StudentSet.add(new Student(2, "Ankit")); StudentSet.add(new Student(3, "Abhishek")); StudentSet.add(new Student(1, "vikas")); // Printing the Output for (Student student : StudentSet) System.out.println("StudentID: " + student.getId() + ", Name: " + student.getName()); } } // Custom Object Class class Student { private int id; private String name; // Constructor public Student(int id, String name) { this.id = id; this.name = name; } // Override equals method for custom criteria public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student)o; return id == student.id; } // Overriding hashCode method public int hashCode() { return Objects.hash(id); } public int getId() { return id; } public String getName() { return name; } } OutputStudentID: 1, Name: Amit StudentID: 2, Name: Ankit StudentID: 3, Name: Abhishek Explanation of the above Program:We have created a custom class of Student type.Then we set a custom criteria by overriding the equals() method.Then we create the get methods.After that we have created the LinkedHashSet of Student type.Then some students came with their unique ID's so the values are stored in the set.But when a student named Vikas came with the same ID which is already present in the set, the value is not updated.As shown in the output. Comment More info D devanshuma3v23 Follow Improve Article Tags : Java Java Programs java-LinkedHashSet Java Examples Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like