HashMap - getting the value of the first key

Below are the values โ€‹โ€‹contained in the HashMap

statusName {Active=33, Renewals Completed=3, Application=15} 

Java code to get the first key (i.e. Active)

 Object myKey = statusName.keySet().toArray()[0]; 

How can we collect the first key "Value" (i.e. 33), I want to save both the "key" and the "value" in a separate variable.

+86
java hashmap
Oct 07 '14 at 6:54
source share
9 answers

You can try the following:

  Map<String,String> map = new HashMap<>(); Map.Entry<String,String> entry = map.entrySet().iterator().next(); String key = entry.getKey(); String value = entry.getValue(); 

Remember that HashMap does not guarantee insertion order. Use LinkedHashMap to keep order.

For example:

  Map<String,String> map = new LinkedHashMap<>(); map.put("Active","33"); map.put("Renewals Completed","3"); map.put("Application","15"); Map.Entry<String,String> entry = map.entrySet().iterator().next(); String key= entry.getKey(); String value=entry.getValue(); System.out.println(key); System.out.println(value); 

Output:

  Active 33 
+178
Oct 07 '14 at 6:57
source share
โ€” -

To get the "first" value:

 map.values().toArray()[0] 

To get the value of the "first" key:

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

Note. The code has been verified and works.

I say "first" because the HashMap entries are not ordered.

However, LinkedHashMap iterates over its entries in the order they were inserted โ€” you can use this for your map implementation if it is important that the insertion order is important.

+45
Oct 07 '14 at 6:59
source share

Java 8 way to do

String firstKey = map.keySet().stream().findFirst().get();

+29
Aug 16 '17 at 4:28
source share

how can we collect the first key "Value" (ie 33)

Using youMap.get(keyYouHave) , you can get its value.

you want to save both "keys" and "value" in a separate variable

Yes, you can assign this to a variable.

Wait ......... this is not the end.

If you (business logic) depend on the insertion and extraction order, you will see strange results. The card is not ordered, they will not be stored in the order. Please keep this in mind. Use the alternative to save your order. Probably LinkedHashMap

+4
Oct 07 '14 at 6:59
source share

You can also try this to get the whole first entry,

 Map.Entry<String, String> entry = map.entrySet().stream().findFirst().get(); String key = entry.getKey(); String value = entry.getValue(); 

This is to get only the key of the first record,

 String key = map.entrySet().stream().map(Map.Entry::getKey).findFirst().get(); // or better String key = map.keySet().stream().findFirst().get(); 

This is to get only the value of the first record,

 String value = map.entrySet().stream().map(Map.Entry::getValue).findFirst().get(); // or better String value = map.values().stream().findFirst().get(); 

Moreover, if you know what you are doing and want to get the second (same for the third, etc.) Map Element, you should try this,

 Map.Entry<String, String> entry = map.entrySet().stream().skip(1).findFirst().get(); String key = map.keySet().stream().skip(1).findFirst().get(); String value = map.values().stream().skip(1).findFirst().get(); 
+4
Aug 22
source share

Remember that the insertion order is not respected on the map at all. Try the following:

  /** * Get the first element of a map. A map doesn't guarantee the insertion order * @param map * @param <E> * @param <K> * @return */ public static <E,K> K getFirstKeyValue(Map<E,K> map){ K value = null; if(map != null && map.size() > 0){ Map.Entry<E,K> entry = map.entrySet().iterator().next(); if(entry != null) value = entry.getValue(); } return value; } 

I only use this when I'm sure it is map.size() == 1 .

+1
Nov 08 '17 at 17:41
source share

Please note that you should note that your logical thread should never rely on accessing the HashMap elements in some order, just put it because the HashMap not ordered by Collection , and thatโ€™s not what they are aimed at. (You can learn more about the odered and sorter collections in this post ).

Back to the message, you have already completed half the task by downloading the first key of the element:

 Object myKey = statusName.keySet().toArray()[0]; 

Just call map.get(key) to get the appropriate value:

 Object myValue = statusName.get(myKey); 
0
Oct 07 '14 at 7:09
source share

Improve, answer. Since findFirst() returns an Optional value, it is recommended that you check to see if the value exists.

  var optional = pair.keySet().stream().findFirst(); if (!optional.isPresent()) { return; } var key = optional.get(); 

Also, some noted that finding the first HashSet key is unreliable. But sometimes we have HashMap pairs; those. in each map we have one key and one value. In such cases, quickly find the first key in such a pair.

0
Jun 30 '19 at 15:01
source share

You can also try:

 Map.Entry<String, Integer> entry = myMap.firstEntry(); System.out.println("First Value = " + entry); 
-2
Jan 31 '17 at 6:57
source share



All Articles