El pocho la pantera
El pocho la pantera

Reputation: 505

Create a hash from a ParameterMap object

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...

Upvotes: 0

Views: 95

Answers (1)

Steve P.
Steve P.

Reputation: 14699

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

Upvotes: 1

Related Questions