Reputation: 43
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
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
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
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:
Map<String, List<String>> applications
object somewhere in your application.For that you can simply use: applications.containsKey(personalIdNo);
.
This does exactly what you seem to be wanting.
Upvotes: 0
Reputation: 178293
The Map
interface supplies the containsKey
method. Try
!applications.containsKey(personalIdNo)
Upvotes: 5