|
|
Program: How to find user defined objects as a key from LinkedHashMap?
Description: |
Below example shows how to search 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 MyObjectKeySearch {
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("Does key available? "+hm.containsKey(key));
}
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(){
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
|
|
|
|
|
List Of All LinkedHashMap Sample Programs:- LinkedHashMap basic operations
- How to iterate through LinkedHashMap?
- How to check whether the value exists or not in a LinkedHashMap?
- How to delete all entries from LinkedHashMap object?
- How to eliminate duplicate user defined objects as a key from LinkedHashMap?
- How to find user defined objects as a key from LinkedHashMap?
- How to delete user defined objects as a key from LinkedHashMap?
|
|
|
When to use LinkedList or ArrayList?
Accessing elements are faster with ArrayList, because it is index based.
But accessing is difficult with LinkedList. It is slow access. This is
to access any element, you need to navigate through the elements one by
one. But insertion and deletion is much faster with LinkedList, because
if you know the node, just change the pointers before or after nodes.
Insertion and deletion is slow with ArrayList, this is because, during
these operations ArrayList need to adjust the indexes according to
deletion or insetion if you are performing on middle indexes. Means,
an ArrayList having 10 elements, if you are inserting at index 5, then
you need to shift the indexes above 5 to one more.
The pessimist complains about the wind; the optimist expects it to change; the realist adjusts the sails.
-- William Arthur Ward
|