JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to fetch values with user define object as keys in Hashtable?


Description:

Below example shows how to use user defined objects as keys and fetch values. 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;

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
What is race condition?
A race condition is a situation in which two or more threads or processes are reading or writing some shared data, and the final result depends on the timing of how the threads are scheduled. Race conditions can lead to unpredictable results and subtle program bugs. A thread can prevent this from happening by locking an object. When an object is locked by one thread and another thread tries to call a synchronized method on the same object, the second thread will block until the object is unlocked.
Famous Quotations
It’s not that I’m so smart, it’s just that I stay with problems longer.
-- 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 © 2020 by Nataraja Gootooru. All Rights Reserved.