jay4399
jay4399

Reputation: 43

Need a alternative to key.equals() java

Is there any alternatives to key.equals() thing in Java . here is the code where i need the alternative

   public boolean containsElement(String personalIdNo)
{
    for (Map.Entry<String, ArrayList <String>> entry : applications.entrySet())
    {
        String key = entry.getKey();
        if (key.equals(personalIdNo))
        {
            return false;
        }
    }
    return true;
}

Cheers.

Upvotes: 0

Views: 185

Answers (4)

dev2d
dev2d

Reputation: 4262

i can see the key is a String, depending on your requirements you can use == (for object reference comparison and) and .equals()(and similar equalsIgnoreCase() methods) (for object value comparison.) there is no other alternative for comparison

Upvotes: 0

Rogue
Rogue

Reputation: 11483

There is not really an alternative, .equals() is the comparing function you want. There is also .equalsIgnoreCase() if you want to compare without case.

Another note, you can simply return like so:

return !key.equals(personalIdNo);

Upvotes: 0

skiwi
skiwi

Reputation: 69349

What exactly are you trying to achieve? It is considered hard to answer questions without knowing the exact question.

To me it currently seems that you are implementing behaviour that already exists:

  • I think you have a Map<String, List<String>> applications object somewhere in your application.
  • It seems that you want to return whether or not a key exists.

For that you can simply use: applications.containsKey(personalIdNo);.
This does exactly what you seem to be wanting.

Upvotes: 0

rgettman
rgettman

Reputation: 178293

The Map interface supplies the containsKey method. Try

!applications.containsKey(personalIdNo)

Upvotes: 5

Related Questions