Checkout for Promo Codes

Program: How to eliminate duplicate keys (user defined objects) with Hashtable?


Below example shows how to use user defined objects as keys and delete entries. To do this, you have to implement equals and hashcode methods. In below example we are using Emp object as key, and it has implemented equals, and hashcode methods.

package com.java2novice.hashtable;

import java.util.Hashtable;
import java.util.Set;

public class MyHashtableDupEntry {

	public static void main(String a[]){

		Hashtable<Empl,String> tm = new Hashtable<Empl, String>();
		tm.put(new Empl(134,"Ram",3000), "RAM");
		tm.put(new Empl(235,"John",6000), "JOHN");
		tm.put(new Empl(876,"Crish",2000), "CRISH");
		tm.put(new Empl(512,"Tom",2400), "TOM");
		System.out.println("Adding duplicate entry:");
		tm.put(new Empl(512,"Tom",2400), "TOM");
		System.out.println("Hashtable entries:");
		Set<Empl> keys = tm.keySet();
		for(Empl key:keys){
			System.out.println(key+" ==> "+tm.get(key));
		System.out.println("Duplicate got eliminated!!!");

class Empl{
	private String name;
	private int salary;
	private int id;
	public Empl(int id, String n, int s){ = id; = n;
		this.salary = s;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public int getSalary() {
		return salary;
	public void setSalary(int salary) {
		this.salary = salary;
	public String toString(){
		return "Id: "" -- Name: "" -- Salary: "+this.salary;

	public void setId(int id) { = id;

	public int getId() {
		return id;
	public int hashCode() {
		System.out.println("In hashcode");
		return this.getId();
	public boolean equals(Object obj) {
		Empl e = null;
		if(obj instanceof Empl){
			e = (Empl) obj;
		System.out.println("In equals");
		if(this.getId() == e.getId()){
			return true;
		} else {
			return false;

In hashcode
In hashcode
In hashcode
In hashcode
Adding duplicate entry:
In hashcode
In equals
Hashtable entries:
In hashcode
In equals
Id: 876 -- Name: Crish -- Salary: 2000 ==> CRISH
In hashcode
In equals
Id: 512 -- Name: Tom -- Salary: 2400 ==> TOM
In hashcode
In equals
Id: 235 -- Name: John -- Salary: 6000 ==> JOHN
In hashcode
In equals
Id: 134 -- Name: Ram -- Salary: 3000 ==> RAM
Duplicate got eliminated!!!
<< Previous Program 

List Of All Hashtable Sample Programs:

  1. Basic Hashtable Operations.
  2. How to iterate through Hashtable?
  3. How to copy Map content to another Hashtable?
  4. How to search a key in Hashtable?
  5. How to search a value in Hashtable?
  6. How to get all keys from Hashtable?
  7. How to get entry set from Hashtable?
  8. How to delete all elements from Hashtable?
  9. How to read elements using Enumeration in Hashtable?
  10. Hashtable implementation with equals and hashcode example.
  11. How to fetch values with user define object as keys in Hashtable?
  12. How to eliminate duplicate keys (user defined objects) with Hashtable?
Knowledge Centre
Can you list serialization methods?
Serialization interface does not have any methods. It is a marker interface. It just tells the JVM that your class can be serializable.
Famous Quotations
Never argue with a fool, onlookers may not be able to tell the difference.
-- Mark Twain

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.