cornercoder
cornercoder

Reputation: 276

Is order of extracted values from same hash map same?

Does hashMapObj.values() return the list in the same order when called nth time as it returned the 1st time?

I understand that the order of values will be different than the insertion order. But will the order differ even when multiple calls are made to values() with no alteration to the hashMap ?

Upvotes: 3

Views: 73

Answers (4)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

The answer depends on what happens in between of the two calls to values():

  • If no modifications are made to the HashMap<K,V>, the order would be the same, because the algorithm that is used to traverse the buckets of the hash map does not have a random component. The order is arbitrary with respect to the items inserted into the map, but it remains the same between iterations.
  • If you make modifications to the map, the order may change, even if the content of the map remains the same (e.g. because you remove and re-insert an item, or insert and then remove an item).

Deleting an re-inserting an item can change the order of items in the same hash bucket, so the order of iteration of values() will change accordingly.

Inserting an item may grow the number of cache buckets and re-hashing, which will not be undone when the same item is removed. That will change the order of values() as well.

Upvotes: 1

NimChimpsky
NimChimpsky

Reputation: 47290

No it is not guaranteed, you need a TreeMap :

https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html

Upvotes: 0

Bimper
Bimper

Reputation: 270

A HashMap makes no guarantee about the order its values are iterated when iterating a Collection through .values(). Only that they are in sync:

Returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

docs

If your elements are comparable, and the speed penalty is acceptable. You can always sort the values into another collection and then obtain deterministic order.


The other answer talks about the order of the keys which is irrelevant here.

Upvotes: 2

lcjury
lcjury

Reputation: 1258

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

http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#values()

You can't wait that values() will return the list in the same order, it could happen but is just not a good idea because it depends on the implementation

Upvotes: 2

Related Questions