Open In App
Related Articles

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

Improve Article
Save Article
Like Article

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.


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 Program  to eliminate duplicate user 
// defined Objects from LinkedHashSet
import java.util.*;
// Java program to illustrate
// overriding of equals and
// hashcode methods
class student {
    int marks;
    String name;
    // Constructor
    public student(String name, int marks)
        this.marks = marks; = name;
    // Getters and Setters
    public int getMarks() { return marks; }
    public String getName() { return name; }
    public void setName(String 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;
    // 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 ( != null)
                return false;
        else if (!name.equals(
            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.marks);


cam 451
sam 452

Last Updated : 07 Jan, 2021
Like Article
Save Article
Similar Reads
Related Tutorials