0

i have objects X and Y of class Container. Both are asociated to a ParameterMap object, wich is a map String-String (request parameter values). let be:

X exampleX;
Y exampleY;

I want to know if exampleX is asociated with the same ParameterMap as exampleY. As i dont really need the value of the map, i thought i could store in class Container a integer, the result of hash function to the parameterMap which the object is asociated. So, if X.parameterMapHash == Y.parameterMapHash, then the maps have the same values for the same keys....

Is this a good approach? How can i make this in java? i need something to make a hash from a object...

1 Answer 1

1

You can use == to determine if two variables reference the same object.

if (X.parameterMapHash == Y.parameterMapHash)
{
    //they reference the same object
    //doSomething
}

However, if you're asking how to figure out if two Maps are entirely equivalent, but do not necessairly reference the same object, then you could do something like:

boolean checkKeysAndValues()  
{  
    if(X.parameterHashMap.size() == Y.parameterHashMap.size())
    {
        for (Map.Entry<String, String> entry : X.parameterHashMap.entrySet())
        {
            if (!(Y.ParameterHashMap.containsKey(entry.getKey()) &&
                Y.ParameterHashMap.get(entry.getKey()).equals(entry.getValue()))
            {            
                return false;
            }
        }
        return true;
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

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.