JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to eliminate duplicate user defined objects as a key from LinkedHashMap?


Description:

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


Code:
package com.java2novice.linkedhashmap;

import java.util.LinkedHashMap;
import java.util.Set;

public class MyDuplicateKeyEx {

	public static void main(String a[]){
		
		LinkedHashMap<Price, String> hm = new LinkedHashMap<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("Adding duplicate key...");
		hm.put(key, "Grape");
		System.out.println("After adding dulicate key:");
		printMap(hm);
	}
	
	public static void printMap(LinkedHashMap<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(){
		int hashcode = 0;
		hashcode = price*20;
		hashcode += item.hashCode();
		return hashcode;
	}
	
	public boolean equals(Object obj){
		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
Adding duplicate key...
After adding dulicate key:
item: Apple  price: 40==>Apple
item: Orange  price: 30==>Orange
item: Banana  price: 20==>Grape
<< Previous Program | Next Program >>

List Of All LinkedHashMap Sample Programs:

  1. LinkedHashMap basic operations
  2. How to iterate through LinkedHashMap?
  3. How to check whether the value exists or not in a LinkedHashMap?
  4. How to delete all entries from LinkedHashMap object?
  5. How to eliminate duplicate user defined objects as a key from LinkedHashMap?
  6. How to find user defined objects as a key from LinkedHashMap?
  7. How to delete user defined objects as a key from LinkedHashMap?
Knowledge Centre
Can interface be final?
No. We can not instantiate interfaces, so in order to make interfaces useful we must create subclasses. The final keyword makes a class unable to be extended.
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 © 2022 by Nataraja Gootooru. All Rights Reserved.