1

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?

2 Answers 2

3

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);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. Can you give me some idea what to do in case of an array?
I meant how to implement the hashcode(). I got the equals() part.
Okay let me clarify once again.
@AhsanTarique If you have a different question, please open a new post for it. It's very hard to follow questions presented like this in the comments without some more context (or code)
Sorry @Mureinik. I'm not sure if it's a different question. This is what I was referring to: class A{ private int arr[]; ...} . If you think this should be asked in a different question please let me know.
|
1

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");
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.