Saturday, April 22, 2023

How to find the First Key or Value from a Map in Java? HashMap Example Tutorial

Hello guys, if you are working with Java HashMap or any other Map implementation like TreeMap or LinkedHashMap and wondering how to get the first enty, first key or first value then you have come to the right place. In my past articles, I have talked about how HashMap works internally as well as we have seen several HashMap examples and in this article, I will show you how to get the first key or value from HashMap in Java. I actually needed this when I was doing a coding exercise online and I was using TreeMap to store objects. Since TreeMap store objects or mapping like key and value in sorted order the first element can be either maximum or minimum depending upon whether you sorted the TreeMap on increasing or decreasing order. 

While with TreeMap it's a lot easier because it implement NavigableMap and SortedMap and provides methods like firstKey() and firstEntry() It wasn't easy for other Map implementation which doesn't guarantee ordering like HashMap but there are scenarios where you don't care about order and just need the first key or value to process. 

It's also a popular HashMap interview question and knowing how to get the first key, value, and entry on different Map implementation like HashMap, TreeMap or LinkedHashMap will also help you during Java interviews. 


How to find the first key or value form HashMap in Java

The easiest way to get the first key or first value from a HashMap in Java is to use the entrySet() method to get a set of key-value pairs, and then use the iterator() method to get an iterator over the set. Finally, you can use the next() method to get the first entry in the Map. 

Here's a code example which demonstrate that:
Map<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);

String firstKey = map.keySet().iterator().next();
System.out.println("First key: " + firstKey);

When you will run this code snippet you will get following output:

Output:
First key: A

Similarly, To find the first value from a Map, you can use the values() method to get a collection of values, and then use the iterator() method to get an iterator over the collection. Finally, you can use the next() method to get the first value in the Map. 

How to find the First Entry, Key or Value from a Map in Java? Example Tutorial



Here's an example:

Map<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);

Integer firstValue = map.values().iterator().next();
System.out.println("First value: " + firstValue);

When you will run this program you will get following output:
First value: 1

That's all about how to get the first key, value and entry from a HashMap in Java. As I said, it depends upon type of Map you are using, if you are using  a sorted Map like TreeMap, you can get the first key and entry easily by using firstKey() and firstEntry() method and then you can get the value from the Entry object.  

For unsorted maps, we can use the iterator() method of the map to get the first entry, key, or value.

In addition, we can also use the entrySet() method to get the set of entries in the map and use the stream() method of Java 8 to filter the first entry based on the conditions specified.

By choosing the appropriate method based on the map's characteristics and requirements, developers can easily retrieve the first entry, key, or value from a map in Java.

Other Java HashMap tutorials you may like to explore:
  • How does get() method of HashMap work in Java? (answer)
  • How to update value for a key in HashMap (update example)
  • Difference between ArrayList and HashMap? (difference)
  • How to use computeIfAbsent() in HashMap? (compute example)
  • 3 ways to loop over a Map in Java? (example)
  • 40 HashMap interview questions with answers (HashMap questions)
  • HashSet vs HashMap in Java? (answer)
  • How HashSet internally works in Java? (answer)
  • How to merge two HashMap in Java? (merge example)
  • HashMap vs LinkedHashMap in Java? (answer)
  • ArrayList vs HashMap in Java? (answer)
  • Difference between ConcurrentHashMap and HashMap in Java? (answer)
  • How to sort the HashMap on keys and values in Java? (solution)
  • How ConcurrentHashMap internally works in Java? (answer)
  • HashMap vs ConcurrentHashMap in Java? (answer)
  • The best way to iterate over HashMap in Java? (answer)
  • Difference between HashMap vs Hashtable in Java? (answer)
  • How to convert Map to List in Java? (solution)

Thanks for reading this article so far. If you like this Java HashMap example then please share with your friends and colleagues. If you have any questions or doubt feel free to ask in comments. 

No comments :

Post a Comment