Since your question is not very specific about what you consider the "first key" I will just list a few options.
Just the first one in the key set
String firstKey = map.keySet().iterator().next();
But no idea what information that provides you.
The smallest key
String firstKey = map.keySet().stream().min(String::compareTo).get();
The key of the smallest value
String firstKey = map.entrySet().stream().min((a,b) -> a.getValue().compareTo(b.getValue())).get().getKey();
The first inserted key
This does not work with a regular HashMap
because it does not preserve the ordering. Use a LinkedHashMap
instead.
Map<String, Double> map = new LinkedHashMap<>();
String firstKey = map.keySet().iterator().next();