I want to avoid the use of for loop for iterating through the HaspMap for finding a key and then picking up the value corresponding to that key…Can anyone help me to implement the same…?
Below given is my code using forloop which i would like to replace with containsKey function()
for (Entry<Long, Long> entry : sessionTimeStampHash.entrySet())
//if(sessionTimeStampHash.containsKey(sessionID))
{
if (entry.getKey().equals(sessionID))
{
sessionTimeStamp = entry.getValue();
}
}
You don’t need a loop at all. Maps are associative collections, which means they are designed and optimized for fast key-based lookup. A map is meant to be used this way:
Or using
containsKey():See the Java Tutorial to learn more about Maps and other collections.