Reputation: 321
Can I access HashMap's first and second elements separately? I mean if I have a HashMap
Map<Integer, Integer> testMap = new HashMap<Integer, Integer>();
int f = testMap.first();
I know that there's no such function, but how can I access that element?
Upvotes: 0
Views: 2414
Reputation: 606
Iterator it = testMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
System.out.println("Key"+pairse.getKey());
System.out.println("Value"+pairs.getValue());
}
Upvotes: 0
Reputation: 41230
HashMap
does not maintain order.
This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
You can use LinkedHashMap
which maintain insertion order or a TreeMap
.
This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.)
The map is sorted according to the natural ordering of its keys, or by a Comparator
provided at map creation time, depending on which constructor is used.
Upvotes: 2
Reputation: 13900
HashMap <Integer, Integer> testMap = new HashMap <Integer, Integer> ();
testMap.put (1, 2);
testMap.put (2, 3);
Iterator <Integer> i = testMap.values ().iterator ();
System.out.println ("First element: " + i.next ());
System.out.println ("Second element: " + i.next ());
Note, that because HashMap
does not remember which order elements were added in, "first element" does not necessary mean "first added".
Upvotes: 0
Reputation: 3649
You can use LinkedHashMap
which has methods like firstKey
and lastKey
.But for 2nd key you have to iterate and you get the element in order in which keys entered in the map
Upvotes: 0
Reputation: 1027
no. you can't as HapMap/HashTable store the data in Entry for (http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Map.Entry.html) which holds key and value together.
you can alway get Iterator of entrySet() and access the entries. Or try to get keySet() which returns set of keys only.
hope this helps.
Upvotes: 0
Reputation: 364988
Maps are not ordered, so there is no such thing as 'the first entry', and that's also why there is no get-by-index method on Map (or HashMap).
You could do this:
Map<Integer, Integer> map = ...; // wherever you get this from
// Get the first entry that the iterator returns
Map.Entry<Integer, Integer> entry = map.entrySet().iterator().next();
(Note: Checking for an empty map omitted).
Your code doesn't get all the entries in the map, it returns immediately (and breaks out of the loop) with the first entry that's found.
Note: Calling iterator()
does not mean that you are iterating over the whole map.
Upvotes: 0