Ahsan Tarique
Ahsan Tarique

Reputation: 651

Mapping objects in Java

I want to implement the following idea in Java: If I map one object of a class having 2 members to a Boolean value, and create another object of the same class with same 2 member values, the second object should map to the same Boolean value as the first one.

Here is the code in C++ that hopefully explains what I'm trying to do:

#include <iostream>
#include <map>

using namespace std;


class A{
    int x;
    int y;

public:
    A(int a, int b){
        x = a;
        y = b;
    }
    bool operator < (const A &another) const{
        return x < another.x || y < another.y;
    }
};


int main() {

    A a(1,2),b(1,2);

    map <A,bool> exists;

    exists[a]=true;

    if(exists[b]){
        cout << "(1,2) exists" << endl;
    }
    else{
        cout << "(1,2) does not exist" << endl;
    }

    return 0;
}

Output:

(1,2) exists

Here a and b are not the same object but they have the same member values. So they map to the same Boolean value.

I have tried using HashMap in Java to implement this without success:

import java.util.*;
import java.lang.*;
import java.io.*;

class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        A a = new A(1,2);
        A b = new A(1,2);

        Map <A,Boolean> exists =  new HashMap<A,Boolean>();

        exists.put(a,true);
        if(exists.containsKey(b)){
            System.out.println("(1,2) exists");
        }
        else{
            System.out.println("(1,2) does not exist");
        }
    }
}

class A{
    private int x;
    private int y;

    public A(int a, int b){
        x = a;
        y = b;
    }
}

Output:

(1,2) does not exist

How should I implement this in Java?

Upvotes: 1

Views: 482

Answers (2)

Noushad
Noushad

Reputation: 512

Your Class, A should over ride the equals and hashcode method.

    public class A {
    private final int x;
    private final int y;

    public A(final int a, final int b) {
        this.x = a;
        this.y = b;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + x;
        result = prime * result + y;
        return result;
    }


    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        A other = (A) obj;
        if (x != other.x)
            return false;
        if (y != other.y)
            return false;
        return true;
    }

}

class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        A a = new A(1,2);
        A b = new A(1,2);

        Map <A,Boolean> exists =  new HashMap<A,Boolean>();

        exists.put(a,true);
        if(exists.containsKey(b)){
            System.out.println("(1,2) exists");
        }
        else{
            System.out.println("(1,2) does not exist");
        }
    }
}

Upvotes: 1

Mureinik
Mureinik

Reputation: 310983

In order to have an object serve as a key in a HasMap you need to override its equals(Object) and hashCode() methods:

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    A a = (A) o;
    return x == a.x &&
            y == a.y;
}

@Override
public int hashCode() {
    return Objects.hash(x, y);
}

Upvotes: 3

Related Questions