How to Eliminate Duplicate User Defined Objects as a Key from Java LinkedHashMap? Last Updated : 20 Feb, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: LinkedHashMap = [{[Apple, 40], Kashmir}, {[Grapes, 80], Delhi}]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. Below is the implementation of the problem statement: Java // Java Program to eliminate duplicate user defined // objects as a key from Java LinkedHashMap import java.util.*; class Employee { private String name; private int id; // Constructor public Employee(String name, int id) { this.name = name; this.id = id; } // HashCode Method public int hashCode() { System.out.println("In hashcode method"); int hashcode = 0; return hashcode; } // Equals Method public boolean equals(Object obj) { System.out.println("In equals method"); if (obj instanceof Employee) { Employee emp = (Employee)obj; return (emp.name.equals(this.name) && emp.id == this.id); } else { return false; } } // Getters and Setters public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String toString() { return "Employee Id: " + id + " Name: " + name; } } // Driver code public class Duplicate_Value { public static void main(String a[]) { // LinkedHashMap initialization LinkedHashMap<Employee, Integer> lhm = new LinkedHashMap<Employee, Integer>(); // Adding entries in LinkedHashMap lhm.put(new Employee("John", 1020), 1); lhm.put(new Employee("Ravi", 1040), 2); lhm.put(new Employee("Jaya", 1030), 3); // Print LinkedHashMap for (Map.Entry<Employee, Integer> entry : lhm.entrySet()) { System.out.println(entry.getKey() + "=>" + entry.getValue()); } // Create duplicate entry Employee duplicate = new Employee("John", 1020); System.out.println("Inserting duplicate record..."); // Add duplicate entry lhm.put(duplicate, 4); System.out.println("After insertion:"); for (Map.Entry<Employee, Integer> entry : lhm.entrySet()) { System.out.println(entry.getKey() + "=>" + entry.getValue()); } } } OutputIn hashcode method In hashcode method In equals method In hashcode method In equals method Employee Id: 1020 Name: John Inserting duplicate record... In hashcode method In equals method After insertion: Employee Id: 1020 Name: JohnTime Complexity: O(1) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Find User Defined Objects From LinkedHashSet in Java? P pawki Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-LinkedHashMap +1 More Practice Tags : Java Similar Reads 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 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 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 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 Elements from a LinkedHashMap in Java? A LinkedHashMap is a part of the Collection Framework from java.util package Java and is similar to a HashMap, except that a LinkedHashMap preserves the insertion order among the keys/entries. In this article, we will look at how to remove the elements from a LinkedHashMap in Java. Program to Remove 2 min read How to Avoid Duplicate User Defined Objects in TreeSet in Java? TreeSet class in Java is part of Javaâs collections framework which implements the NavigableSet interface, which provides functionalities to navigate through the SortedSet. The NavigableSet further extends the SortedSet interface, which provides functionalities to keep the elements sorted. As the Tr 5 min read Like