How to Eliminate Duplicate User Defined Objects from LinkedHashSet in Java?
Last Updated :
11 Mar, 2024
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 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;
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);
}
}
}
Similar Reads
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 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 i
3 min read
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 Remove Duplicate Elements From Java LinkedList? Linked List is a part of the Collection in java.util package. LinkedList class is an implementation of the LinkedList data structure it is a linear data structure. In LinkedList due to the dynamical allocation of memory, insertions and deletions are easy processes. For removing duplicates from Examp
4 min read
How to Remove Duplicate Elements from the Vector in Java? Using LinkedHashSet and TreeSet, duplicate elements are removed. Because the LinkedHashSet and TreeSet do not accept duplicate elements. Example: Input : vector = [1, 2, 3, 4, 2, 4] Output: vector = [1, 2, 3, 4] Input : vector = [a, b, a, c, d, a] Output: vector = [a, b, c, d]Approach 1: Using Linke
3 min read