If you are refering to LinkedHashMap, as mentioned in you previous post:
just take the first key returned by the key set iterator:
Originally posted by Edwin Dalorzo:
As the previous post states, the first key of a HashMap may not necesarily be the firt entry you put into the HashMap, since HashMap do not keep an specific order.
That being said, you can also obtain the first key in different ways. Another way to do it is:
I hope this helps!
If a HashMap makes no guarantee of the order of its elements, is this code guaranteed to return the same result every time ? The JavaDoc for Collection.toArray() says
If the collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
As a Set is not ordered I suspect the answer is 'No', but it would be good if someone could confirm this.