import java.util.HashMap;
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
// Implementing equals() and hashCode() methods
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
if (age != person.age) return false;
return name != null ? name.equals(person.name) : person.name == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
return result;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
class Address {
private String street;
private String city;
private String state;
private int zipCode;
public Address(String street, String city, String state, int zipCode) {
this.street = street;
this.city = city;
this.state = state;
this.zipCode = zipCode;
}
public String getStreet() {
return street;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
public int getZipCode() {
return zipCode;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Address address = (Address) o;
if (zipCode != address.zipCode) return false;
if (street != null ? !street.equals(address.street) : address.street != null) return false;
if (city != null ? !city.equals(address.city) : address.city != null) return false;
return state != null ? state.equals(address.state) : address.state == null;
}
@Override
public int hashCode() {
int result = street != null ? street.hashCode() : 0;
result = 31 * result + (city != null ? city.hashCode() : 0);
result = 31 * result + (state != null ? state.hashCode() : 0);
result = 31 * result + zipCode;
return result;
}
@Override
public String toString() {
return "Address{" +
"street='" + street + '\'' +
", city='" + city + '\'' +
", state='" + state + '\'' +
", zipCode=" + zipCode +
'}';
}
}
public class AdvancedHashMapExample {
public static void main(String[] args) {
HashMap<Person, Address> map = new HashMap<>();
// Adding elements to the HashMap
Person alice = new Person("Alice", 25);
Address aliceAddress = new Address("123 Main St", "Anytown", "CA", 12345);
map.put(alice, aliceAddress);
Person bob = new Person("Bob", 30);
Address bobAddress = new Address("456 Elm St", "Sometown", "NY", 67890);
map.put(bob, bobAddress);
// Printing the
This code defines two custom classes, Person
and Address
, which will be used as keys and values in the HashMap
. The Person
class has a name and an age, and the Address
class has a street, city, state, and ZIP code.
Both classes implement the equals()
and hashCode()
methods, which are necessary for using custom objects as keys in a HashMap
. The equals()
method checks if two objects are equal based on their fields, and the hashCode()
method returns a hash code for the object, which is used to determine the bucket in the hash table where the object should be stored.
The AdvancedHashMapExample
class creates a HashMap
with keys of type Person
and values of type Address
, and adds two key-value pairs to it: alice
maps to aliceAddress
, and bob
maps to bobAddress
.
It then prints out the contents of the HashMap
using a forEach()
loop to iterate over the entries in the HashMap
. For each entry, it prints out the key (Person
object) and the value (Address
object).
The output of this code will be:
{Person{name='Alice', age=25}=Address{street='123 Main St', city='Anytown', state='CA', zipCode=12345}, Person{name='Bob', age=30}=Address{street='456 Elm St', city='Sometown', state='NY', zipCode=67890}}
Person{name='Alice', age=25} => Address{street='123 Main St', city='Anytown', state='CA', zipCode=12345}
Person{name='Bob', age=30} => Address{street='456 Elm St', city='Sometown', state='NY', zipCode=67890}
This example demonstrates how to use custom objects as keys in a HashMap
, and how to properly implement the equals()
and hashCode()
methods.