JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

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


Description:

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.


Code:
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){
		this.id = id;
		this.name = n;
		this.salary = s;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public String toString(){
		return "Id: "+this.id+" -- Name: "+this.name+" -- Salary: "+this.salary;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}
	
	@Override
	public int hashCode() {
		System.out.println("In hashcode");
		return this.getId();
	}
	
	@Override
	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;
		}
		
	}
}

Output:
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
Domain Naming Service(DNS)
It is very difficult to remember a set of numbers (IP address) to connect to the Internet. The Domain Naming Service(DNS) is used to overcome this problem. It maps one particular IP address to a string of characters. For example, www.java2novice.com implies com is the domain name reserved for US commercial sites, java2novice is the name of the company and www is the name of the specific computer, which is java2novice's server.
Famous Quotations
The greatest obstacle to discovery is not ignorance; it is the illusion of knowledge.
-- Daniel J. Boorstin

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