JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Program: Hashtable implementation with equals and hashcode example.


Description:

Below example shows how to use user defined objects as keys. To avoid duplicates keys 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. Example also enters duplicate object as key, and you can see that Hashtable eleminates the duplicate keys.


Code:
package com.java2novice.hashtable;

import java.util.Hashtable;

public class MyHashtableUserKeys {

	public static void main(String a[]){

		Hashtable<Emp,String> tm = new Hashtable<Emp, String>();
		tm.put(new Emp(134,"Ram",3000), "RAM");
		tm.put(new Emp(235,"John",6000), "JOHN");
		tm.put(new Emp(876,"Crish",2000), "CRISH");
		tm.put(new Emp(512,"Tom",2400), "TOM");
		System.out.println("Fecthing value by creating new key:");
		
		Emp e = new Emp(512,"Tom",2400);
		System.out.println(e+" ==> "+tm.get(e));
	}
}

class Emp{
	
	private String name;
	private int salary;
	private int id;
	
	public Emp(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) {
		Emp e = null;
		if(obj instanceof Emp){
			e = (Emp) 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
Fecthing value by creating new key:
In hashcode
In equals
Id: 512 -- Name: Tom -- Salary: 2400 ==> TOM
<< Previous Program | Next 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
If you don’t make mistakes, you’re not working on hard enough problems. And that’s a big mistake.
-- Frank Wilczek

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.