import
java.util.HashMap;
public
class
GfGCustomHashCode
{
public
static
void
main(String[] args) {
HashMap<CustomKey, String> map =
new
HashMap<>();
CustomKey key1 =
new
CustomKey(
"key1"
);
CustomKey key2 =
new
CustomKey(
"key2"
);
CustomKey key3 =
new
CustomKey(
"key3"
);
map.put(key1,
"Java"
);
map.put(key2,
"JavaScript"
);
map.put(key3,
"Java"
);
System.out.println(map);
System.out.println(map.get(key1));
System.out.println(map.get(key2));
System.out.println(map.get(key3));
}
}
class
CustomKey
{
private
String keyData;
public
CustomKey(String keyData)
{
this
.keyData = keyData;
}
@Override
public
int
hashCode() {
return
keyData.hashCode();
}
@Override
public
boolean
equals(Object obj) {
if
(
this
== obj) {
return
true
;
}
if
(obj ==
null
|| getClass() != obj.getClass()) {
return
false
;
}
CustomKey other = (CustomKey) obj;
return
keyData.equals(other.keyData);
}
@Override
public
String toString() {
return
keyData;
}
}