How to Eliminate Duplicate User Defined Objects from LinkedHashSet in Java?

07 Jan, 2021
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 must be the same. If two objects are unequal according to the equals() method, their hash codes are not required to be different.
Syntax:
equals() Method:
public boolean equals  (Object obj)

// This method checks if some other Object
// passed to it as an argument is equal to 
// the Object on which it is invoked.
HashCode() Method:
public int hashCode()

// This method returns the hash code value 
// for the object on which this method is invoked.
Whenever it(hashcode) is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified.
If two objects are equal according to the equals(Object) method, then calling the hashCode() method on each of the two objects must produce the same integer result.
Below is the implementation of the above problem statement:
Java
// Java Program  to eliminate duplicate user // defined Objects from LinkedHashSetimport java.util.*;// Java program to illustrate// overriding of equals and// hashcode methodsclass student {    int marks;    String name;    // Constructor    public student(String name, int marks)    {        this.marks = marks;        this.name = name;    }    // Getters and Setters    public int getMarks() { return marks; }    public String getName() { return name; }    public void setName(String name) { this.name = name; }    public void setMarks(int marks) { this.marks = marks; }    @Override public int hashCode()    {        final int prime = 31;        int result = 1;        result = prime * result + marks;        result = prime * result                 + ((name == null) ? 0 : name.hashCode());        return result;    }    @Override    // if both the object references are    // referring to the same object.    public boolean equals(Object obj)    {        if (this == obj)            return true;        if (obj == null)            return false;        if (getClass() != obj.getClass())            return false;          // type casting of the argument.        student other = (student)obj;          // comparing the state of argument with        // the state of 'this' Object        if (marks != other.marks)            return false;        if (name == null) {            if (other.name != null)                return false;        }        else if (!name.equals(other.name))            return false;        return true;    }}  public class GFG {      public static void main(String[] args)    {        // HashSet initialization        HashSet<student> set = new HashSet<>();          // Add entries in HashSet        set.add(new student("sam", 452));        set.add(new student("cam", 451));        set.add(new student("sam", 452));        set.add(new student("cam", 451));        for (student std : set) {            System.out.println(std.name + " " + std.marks);        }    }}
cam 451
sam 452
java-LinkedHashSet
Picked
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
HashMap in Java with Examples
Stream In Java
ArrayList in Java
Collections in Java
Initializing a List in Java
Java Programming Examples
Convert a String to Character Array in Java
Convert Double to Integer in Java
Implementing a Linked List in Java using Class