/
TestAccount.java
101 lines (72 loc) · 1.78 KB
/
TestAccount.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.kush;
import java.util.HashMap;
import java.util.Map;
public class TestAccount {
public static void main(String[] args) {
Account acc1 = new Account();
acc1.setId("1");
acc1.setName("Kush");
Account acc2 = new Account();
acc2.setId("2");
acc2.setName("Luv");
HashMap<Account, String> hm = new HashMap();
hm.put(acc1, acc1.getName());
hm.put(acc2, acc2.getName());
for (Map.Entry entry : hm.entrySet())
System.out.println("Key: [" + entry.getKey() + "] and value is: " + entry.getValue());
Account acc3 = new Account();
acc3.setId("1");
acc3.setName("Sak");
hm.put(acc3, acc3.getName());
System.out.println("After again duplicate object addition:");
for (Map.Entry entry : hm.entrySet())
System.out.println("Key: [" + entry.getKey() + "] and value is: " + entry.getValue());
}
}
class Account {
private String id;
private String name;
public Account() {
}
public Account(String id, String name) {
this.id = id;
this.name = name;
}
public String toString() {
return "id = " + this.id + "name = " + this.name;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return this.id;
}
public String getName() {
return this.name;
}
@Override
public int hashCode() {
/* final int prime = 31;
int result = 1;
result = prime * result + Integer.parseInt(this.id);
return result;*/
return id.hashCode();
}
// Compare only account id
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Account other = (Account) obj;
if (id.equals(other.id))
return true;
return false;
}
}