JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to find user defined objects as a key from HashMap?


Description:

Below example shows how to search user defined objects as a key from HashMap. You can achieve this by implementing equals and hashcode methods at the user defined objects.


Code:
package com.java2novice.hashmap;

import java.util.HashMap;
import java.util.Set;

public class MyObjectKeySearch {

public static void main(String a[]){
		
		HashMap<Price, String> hm = new HashMap<Price, String>();
		hm.put(new Price("Banana", 20), "Banana");
		hm.put(new Price("Apple", 40), "Apple");
		hm.put(new Price("Orange", 30), "Orange");
		printMap(hm);
		Price key = new Price("Banana", 20);
		System.out.println("Does key available? "+hm.containsKey(key));
	}
	
	public static void printMap(HashMap<Price, String> map){
		
		Set<Price> keys = map.keySet();
		for(Price p:keys){
			System.out.println(p+"==>"+map.get(p));
		}
	}
}

class Price{
	
	private String item;
	private int price;
	
	public Price(String itm, int pr){
		this.item = itm;
		this.price = pr;
	}
	
	public int hashCode(){
		System.out.println("In hashcode");
		int hashcode = 0;
		hashcode = price*20;
		hashcode += item.hashCode();
		return hashcode;
	}
	
	public boolean equals(Object obj){
		System.out.println("In equals");
		if (obj instanceof Price) {
			Price pp = (Price) obj;
			return (pp.item.equals(this.item) && pp.price == this.price);
		} else {
			return false;
		}
	}
	
	public String getItem() {
		return item;
	}
	public void setItem(String item) {
		this.item = item;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	
	public String toString(){
		return "item: "+item+"  price: "+price;
	}
}

Output:
item: Apple  price: 40==>Apple
item: Orange  price: 30==>Orange
item: Banana  price: 20==>Banana
Does key available? true
<< Previous Program | Next Program >>

List Of All HashMap Sample Programs:

  1. Basic HashMap Operations.
  2. How to iterate through HashMap?
  3. How to copy Map content to another HashMap?
  4. How to search a key in HashMap?
  5. How to search a value in HashMap?
  6. How to get all keys from HashMap?
  7. How to get entry set from HashMap?
  8. How to delete all elements from HashMap?
  9. How to eliminate duplicate user defined objects as a key from HashMap?
  10. How to find user defined objects as a key from HashMap?
  11. How to delete user defined objects as a key from HashMap?
Knowledge Centre
wait Vs sleep methods
sleep(): It is a static method on Thread class. It makes the current thread into the "Not Runnable" state for specified amount of time. During this time, the thread keeps the lock (monitors) it has acquired.

wait(): It is a method on Object class. It makes the current thread into the "Not Runnable" state. Wait is called on a object, not a thread. Before calling wait() method, the object should be synchronized, means the object should be inside synchronized block. The call to wait() releases the acquired lock.
Famous Quotations
It is easier to fight for one’s principles than to live up to them.
-- Alfred Adler

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.