0

I want to get the first value from my HashMap while I do not know the key for the value. Is it possible? Are there are libraries to do this?

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
saha
  • 93
  • 1
  • 5
  • 11
  • 4
    What do you mean by first value? First value inserted or first value with a specific criteria? – Alexis C. Apr 25 '14 at 22:10
  • http://stackoverflow.com/questions/46898/how-do-i-iterate-over-each-entry-in-a-map – Joachim Isaksson Apr 25 '14 at 22:11
  • HashMap is Key, Value pair Right?, If i have 10 pairs, I need to get the first value without iterating the rest since i am dealing with a huge amount of data, I don't want to iterate the rest. – saha Apr 25 '14 at 22:18
  • @saha HashMap does not have an order, it's not like a List where you can call get(0). That's why I'm asking what do you mean by first value? Is it the first inserted or it can be any random one? – Alexis C. Apr 25 '14 at 22:19
  • Saha, what exactly do you mean by 'first value'? – Viet Norm Apr 25 '14 at 22:20
  • Sorry, I would like to say the first pair @VietNorm – saha Apr 25 '14 at 22:21
  • Then you need to use a linked hashmap, a hashmap has no iteration order. Are you using the key elsewhere? Perhaps you should use a list? – GoldenJam Apr 25 '14 at 22:22
  • Or simply Set map.keySet() , get the first ValueType and map.get(ValueType) ? But the answer with the LinkedHashMap is much elegenanter. – Peter Apr 25 '14 at 22:26

3 Answers3

3

You could use a java.util.LinkedHashMap<K, V>. Then you can iterate through the map in insertion order.

To only get the first entry you can use an iterator:

Map<String, String > map = new LinkedHashMap<String, String >();
// ... fill the map
Entry<String, String > next = map.entrySet().iterator().next();
next.getKey();
next.getValue();
tangens
  • 39,095
  • 19
  • 120
  • 139
0

If you want the first key that you inserted into the map, then use LinkedHashMap. It's basically a HashMap that remembers the order in which things were inserted, so you can iterate them in the original order.

If you want to select the first key in the natural ordering of the key class; or if you want to select the first key under some ordering of your own, then use TreeMap. It's a type of map that sorts things as you insert them into the map. You can set up a TreeMap with its own Comparator, if you want to specify the order in which the keys are sorted. If you don't supply a Comparator when you create a TreeMap, then it will sort the keys by their natural order.

I recommend reading the Javadocs for both TreeMap and LinkedHashMap before you decide which of the two is appropriate for your application.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
0

Use a LinkedHashMap or TreeMap to maintain insertion order in the first place, after that it is just a matter of using the Iterator.

You can do something like this.

Iterator it = map.entrySet().iterator();
    while (it.hasNext()) {
        //This will be your very first element
        Map.Entry pairs = (Map.Entry)it.next(); 
        System.out.println(pairs.getKey() + " = " + pairs.getValue());
    }
Arif Nadeem
  • 8,524
  • 7
  • 47
  • 78