I have a class
public class Customer {
private int customerId;
private String customerName;
private String customerType;
private String customerAddress;
public Customer(int customerId, String customerName, String customerType, String customerAddress) {
super();
this.customerId = customerId;
this.customerName = customerName;
this.customerType = customerType;
this.customerAddress = customerAddress;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerType() {
return customerType;
}
public void setCustomerType(String customerType) {
this.customerType = customerType;
}
public String getCustomerAddress() {
return customerAddress;
}
public void setCustomerAddress(String customerAddress) {
this.customerAddress = customerAddress;
}
@Override
public String toString() {
return "Customer [customerId=" + customerId + ", customerName=" + customerName + ", customerType="
+ customerType + ", customerAddress=" + customerAddress + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((customerAddress == null) ? 0 : customerAddress.hashCode());
result = prime * result + ((customerName == null) ? 0 : customerName.hashCode());
result = prime * result + ((customerType == null) ? 0 : customerType.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Customer other = (Customer) obj;
if (customerAddress == null) {
if (other.customerAddress != null)
return false;
} else if (!customerAddress.equals(other.customerAddress))
return false;
if (customerName == null) {
if (other.customerName != null)
return false;
} else if (!customerName.equals(other.customerName))
return false;
if (customerType == null) {
if (other.customerType != null)
return false;
} else if (!customerType.equals(other.customerType))
return false;
return true;
}
}
Notice that I have removed the customerId from equal and hashcode calculation. I Created this method to using customer object as a key
public static Map<Customer, String> testKeysWithObject(){
Map<Customer, String> map = new HashMap<>();
Customer customer1 = new Customer(1, "customerName1", "customerType1", "customerAddress1");
Customer customer2 = new Customer(2, "customerName2", "customerType2", "customerAddress2");
Customer customer3 = new Customer(3, "customerName3", "customerType3", "customerAddress3");
Customer customer4 = new Customer(4, "customerName4", "customerType4", "customerAddress4");
map.put(customer1, "customer1");
map.put(customer2, "customer2");
map.put(customer3, "customer3");
map.put(customer4, "customer4");
customer4 = new Customer(5, "customerName5", "customerType5", "customerAddress5");
customer3.setCustomerAddress("customerAddress5");
System.out.println(customer4.getCustomerAddress());
return map;
}
And the below method to traverse the Hashmap.
public static void displayMap(Map<Customer, String> map) {
System.out.println("================================== ENTRY SET ==========================================");
for (Entry<Customer, String> mapKeys : map.entrySet()) {
if(null != mapKeys)
System.out.println("Key -> " + mapKeys.getKey() + " Value -> " + mapKeys.getValue()+ " HashCode -> " + mapKeys.hashCode());
}
System.out.println();
System.out.println("================================== KEY SET ==========================================");
for (Customer mapKeys : map.keySet()) {
if(null != map.get(mapKeys))
System.out.println("Key -> " + mapKeys + " Value -> " + map.get(mapKeys) + " HashCode -> " + map.get(mapKeys).hashCode());
}
}
and below is the output.
customerAddress5
================================== ENTRY SET ========================================== Key -> Customer [customerId=3, customerName=customerName3,
customerType=customerType3, customerAddress=customerAddress5] Value -> customer3 HashCode -> 291012570 Key -> Customer [customerId=4, customerName=customerName4, customerType=customerType4, customerAddress=customerAddress4] Value -> customer4 HashCode -> 291011640 Key -> Customer [customerId=2, customerName=customerName2, customerType=customerType2, customerAddress=customerAddress2] Value -> customer2 HashCode -> 291210360 Key -> Customer [customerId=1, customerName=customerName1, customerType=customerType1, customerAddress=customerAddress1] Value -> customer1 HashCode -> 291211416================================== KEY SET ========================================== Key -> Customer [customerId=4, customerName=customerName4,
customerType=customerType4, customerAddress=customerAddress4] Value -> customer4 HashCode -> 1611562006 Key -> Customer [customerId=2, customerName=customerName2, customerType=customerType2, customerAddress=customerAddress2] Value -> customer2 HashCode -> 1611562004 Key -> Customer [customerId=1, customerName=customerName1, customerType=customerType1, customerAddress=customerAddress1] Value -> customer1 HashCode -> 1611562003
I have a couple of question on this hashmap behavior
- why is hashmap not affected by customer4=new assignment, how does hashcode stores these.
- How is hashmap effected by customer3.setCustomerAddress("customerAddress5");
- Why there are two different values returned by keyset() and entryset methods.
- Does hashmap store reference for actual objects, if references then why customer4 = new had no impact on hashmap?