Publish Your Article Here

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
Default value of a local variables?
The local variables are not initialized to any default values. We should not use local variables with out initialization. Even the java compiler throws error.
Famous Quotations
Insanity: doing the same thing over and over again and expecting different results.
-- Albert Einstein

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 © 2022 by Nataraja Gootooru. All Rights Reserved.