Internal working of HashMap through Program in java
package govind;
public class ProductClass {
String ppname;
float pprice;
public ProductClass()
{
}
public ProductClass(String ppname, float pprice) {
super();
this.ppname = ppname;
this.pprice = pprice;
}
@Override
public int hashCode() {
System.out.println("Ïnside hashcode method");
final int prime = 31;
int result = 1;
result = prime * result + ((ppname == null) ? 0 : ppname.hashCode());
result = prime * result + Float.floatToIntBits(pprice);
return result;
}
@Override
public boolean equals(Object obj) {
System.out.println("Inside equals method");
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ProductClass other = (ProductClass) obj;
if (ppname == null) {
if (other.ppname != null)
return false;
} else if (!ppname.equals(other.ppname))
return false;
if (Float.floatToIntBits(pprice) != Float.floatToIntBits(other.pprice))
return false;
return true;
}
}
=======================================================================================================================================
package govind;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class ProductClassMain {
public static void main(String[] args) {
Map< ProductClass, String> map=new HashMap<>();
map.put(new ProductClass("Nokia", 45000.00f),"Mobile");
map.put(new ProductClass("Dell", 50000.00f),"Laptop");
Set<Entry<ProductClass, String>> set = map.entrySet();
Iterator<Entry<ProductClass, String>> it = set.iterator();
while (it.hasNext()) {
Entry<ProductClass, String> kk = it.next();
ProductClass p = kk.getKey();
System.out.println(p.ppname+" "+p.pprice);
System.out.println(kk.getValue());
}
System.out.println("Try to Insert Duplicate key");
map.put(new ProductClass("Nokia", 45000.00f),"Mobile");
}
}
==========================================================================================================================================
o/p:-----------------------------------
Ïnside hashcode method
Ïnside hashcode method
Dell 50000.0
Laptop
Nokia 45000.0
Mobile
Try to Insert Duplicate key
Ïnside hashcode method
Inside equals method
package govind;
public class ProductClass {
String ppname;
float pprice;
public ProductClass()
{
}
public ProductClass(String ppname, float pprice) {
super();
this.ppname = ppname;
this.pprice = pprice;
}
@Override
public int hashCode() {
System.out.println("Ïnside hashcode method");
final int prime = 31;
int result = 1;
result = prime * result + ((ppname == null) ? 0 : ppname.hashCode());
result = prime * result + Float.floatToIntBits(pprice);
return result;
}
@Override
public boolean equals(Object obj) {
System.out.println("Inside equals method");
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ProductClass other = (ProductClass) obj;
if (ppname == null) {
if (other.ppname != null)
return false;
} else if (!ppname.equals(other.ppname))
return false;
if (Float.floatToIntBits(pprice) != Float.floatToIntBits(other.pprice))
return false;
return true;
}
}
=======================================================================================================================================
package govind;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class ProductClassMain {
public static void main(String[] args) {
Map< ProductClass, String> map=new HashMap<>();
map.put(new ProductClass("Nokia", 45000.00f),"Mobile");
map.put(new ProductClass("Dell", 50000.00f),"Laptop");
Set<Entry<ProductClass, String>> set = map.entrySet();
Iterator<Entry<ProductClass, String>> it = set.iterator();
while (it.hasNext()) {
Entry<ProductClass, String> kk = it.next();
ProductClass p = kk.getKey();
System.out.println(p.ppname+" "+p.pprice);
System.out.println(kk.getValue());
}
System.out.println("Try to Insert Duplicate key");
map.put(new ProductClass("Nokia", 45000.00f),"Mobile");
}
}
==========================================================================================================================================
o/p:-----------------------------------
Ïnside hashcode method
Ïnside hashcode method
Dell 50000.0
Laptop
Nokia 45000.0
Mobile
Try to Insert Duplicate key
Ïnside hashcode method
Inside equals method