I am looking to retrieve key from the value for a SingletonMap
Map<String,String> map = Collections.singletonMap("key1", "value1");
I am looking for a easy way to get the key, based on value. Rather then way that I have done before. Which I feel is too much overhead for a singleTon Map.
public static List<String> getKey(String value, Map<String, String> map)
{
List<String> keys = new ArrayList<String>();
for(Entry<String, String> entry:map.entrySet())
{
if(value.equals(entry.getValue()))
{
keys.add(entry.getKey());
}
}
return keys;
}
Any inputs or suggestion will be helpful.
A singleton map has exactly one entry in it, so you can just do this:
Map<String,String> map = Collections.singletonMap("key1", "value1");
String theOnlyKeyInTheMap = map.keySet().iterator().next();