14

I have the following HashMap<String, Double> map = new HashMap<String, Double>();

How can i get the first key without iterating over it like this:

Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pair = (Map.Entry)it.next();
    System.out.println(pair.getKey() + " = " + pair.getValue());
    it.remove(); 
}

Thanks

Jürgen K.
  • 3,427
  • 9
  • 30
  • 66
  • 1
    Possible duplicate of [Get keys from HashMap in Java](http://stackoverflow.com/questions/10462819/get-keys-from-hashmap-in-java) – J. Su. Feb 26 '16 at 18:02
  • 3
    What is `the first key`? The smallest one? The first one you inserted? The first one you get when iterating over the key set? – MartinS Feb 26 '16 at 18:03

7 Answers7

11

To get the value of the "first" key, you can use it

map.get(map.keySet().toArray()[0]);

In Java8,

You can use stream. For TreeMap/LinkedHashMap, where ordering is significant, you can write

map.entrySet().stream().findFirst();

For HashMap, there is no order, so findAny() might return a different result on different calls

map.entrySet().stream().findAny();
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
8

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();
MartinS
  • 2,759
  • 1
  • 15
  • 25
5

if you use Java 8,

map.entrySet().stream().findFirst().get().getKey()
Community
  • 1
  • 1
karthik r
  • 989
  • 13
  • 11
2
Map<String, Double> map=new HashMap<>();
Map.Entry<String, Double> entry=map.entrySet().iterator().next();
 String key= entry.getKey();

But HashMap doesn't maintain insertion order. You can use LinkedHashMap instead of HashMap.

SkyWalker
  • 28,384
  • 14
  • 74
  • 132
Rahman
  • 3,755
  • 3
  • 26
  • 43
1

Just call it.next() once, be sure the iterator is just in the first position.

Iterator it = map.entrySet().iterator();
Map.Entry firstEntry = (Map.Entry)it.next();
pvxe
  • 90
  • 2
  • 9
0

If you want to iterate over the Map and expect to iterate over it in the order you inserted entries, you should use a LinkedHashMap instead of a HashMap.

This is assuming "first key" means the first one you inserted. Then you can iterate over it in the order you inserted entries, or if you just want the first key like you said, you can call map.keySet().toArray()[0].

Evan LaHurd
  • 977
  • 2
  • 14
  • 27
0

Try using LinkedHashMap instead of HashMap, It maintains the order of insertion when calling keySet().

LinkedHashMap