How to use a custom type as a map key in Java

2 Answers

0 votes
import java.util.Objects;
import java.util.HashMap;
import java.util.Map;
 
class CustomKey {
    private String id;
    private int age;
 
    public CustomKey(String id, int age) {
        this.id = id;
        this.age = age;
    }
 
    // Override equals() to compare objects logically
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CustomKey that = (CustomKey) o;
        return age == that.age && Objects.equals(id, that.id);
    }
 
    // Override hashCode() to generate consistent hash codes
    @Override
    public int hashCode() {
        return Objects.hash(id, age);
    }
 
    @Override
    public String toString() {
        return "CustomKey{" + "id='" + id + '\'' + ", age=" + age + '}';
    }
}
 
 
public class Main {
    public static void main(String[] args) {
        Map<CustomKey, String> map = new HashMap<>();
 
        CustomKey key1 = new CustomKey("A", 1);
        CustomKey key2 = new CustomKey("B", 2);
 
        map.put(key1, "Value for Key1");
        map.put(key2, "Value for Key2");
 
        // Print all entries in the map
        for (Map.Entry<CustomKey, String> entry : map.entrySet()) {
            CustomKey key = entry.getKey();
            String value = entry.getValue();
            System.out.println("Key: " + key + " => Value: " + value);
        }
    }
}
 
 
 
/*
run:
 
Key: CustomKey{id='A', age=1} => Value: Value for Key1
Key: CustomKey{id='B', age=2} => Value: Value for Key2
 
*/

 



answered Aug 11, 2025 by avibootz
edited Aug 11, 2025 by avibootz
0 votes
import java.util.Objects;
import java.util.HashMap;
import java.util.Map;

class CustomKey {
    private String id;
    private int age;

    public CustomKey(String id, int age) {
        this.id = id;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public int getValue() {
        return age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CustomKey that = (CustomKey) o;
        return age == that.age && Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, age);
    }

    @Override
    public String toString() {
        return "CustomKey{" + "id='" + id + '\'' + ", age=" + age + '}';
    }
}

public class Main {
    public static void main(String[] args) {
        Map<CustomKey, String> map = new HashMap<>();

        CustomKey key1 = new CustomKey("A", 1);
        CustomKey key2 = new CustomKey("B", 2);

        map.put(key1, "Value for Key1");
        map.put(key2, "Value for Key2");

        for (Map.Entry<CustomKey, String> entry : map.entrySet()) {
            CustomKey key = entry.getKey();
            String value = entry.getValue();
            System.out.println("Key: " + key);
            System.out.println("  ID: " + key.getId());
            System.out.println("  Age: " + key.getValue());
            System.out.println("  Associated Map Value: " + value);
            System.out.println();
        }
    }
}




/*
run:

Key: CustomKey{id='A', age=1}
  ID: A
  Age: 1
  Associated Map Value: Value for Key1

Key: CustomKey{id='B', age=2}
  ID: B
  Age: 2
  Associated Map Value: Value for Key2

*/

 



answered Aug 11, 2025 by avibootz

Related questions

3 answers 129 views
1 answer 76 views
2 answers 161 views
2 answers 138 views
2 answers 150 views
...