0

I have doubt. If I use two HashMap, store the same thing in that in the same order. Can i confirmly say that both the HashMap has stored the data in the same order.

jontro
  • 9,267
  • 5
  • 37
  • 65
Jatin Khurana
  • 1,125
  • 8
  • 14
  • possible duplicate of [is the Java HashMap keySet() iteration order consistent?](http://stackoverflow.com/questions/1882762/is-the-java-hashmap-keyset-iteration-order-consistent) – Joe Aug 02 '14 at 13:15
  • I must say that this question is unusual. It looks like [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) so could you add some additional informations about real problem you want to solve? – Pshemo Aug 02 '14 at 13:32

4 Answers4

2

If both HashMap instances are instantiated with the same values (size, load factory) and then values with keys are inserted in the same order, then iteration order of the entries will be the same.

But the real question is why does it matter? While we can state that they will both be in the same order, we cannot say what that order will be. If order matters, you need to look at a SortedMap (ordered by Comparator) or a LinkedHashMap (ordered by insert order).

Brett Okken
  • 5,929
  • 1
  • 17
  • 24
1

HashMap makes no guarantee of order but TreeMap does.

Khary Mendez
  • 1,738
  • 1
  • 9
  • 15
1

I assume that this is more theoretical question than real word problem. If you are going do dependent on order of elements in Map you should use instance which guarantees its order in a way you want it like LinkedHashMap uses order of placing elements. Anyway here goes my answer:


I would say Yes. If you provide all exactly same data to algorithm (in this case sorting algorithm) twice and algorithm is only based on this data you should get exactly same results.

HashMas use only one algorithm to sort its elements. Their order will not be natural for you (this map will not sort objects by its properties defined by you) because it will depend on results of hashcode of object you pass as keys.
So if you pass exact same objects to both maps (instances of same class, state and identity) you should get same order as long these objects are guaranteed to have correctly implemented hashcode (and equals) methods. You will not get same order if for instance result of hashcode will depend on time (which is wrong implementation of hashcode method), this kind of hashcode will break HashMap (it will not be able to correctly find its elements).

Pshemo
  • 113,402
  • 22
  • 170
  • 242
  • I am using key as a String class in HashMap so hashcode() mehtod is implemented by String Class creator. So in this case can in say it's same order. – Jatin Khurana Aug 02 '14 at 13:19
  • If the objects used for keys have an invalid `hashcode` and/or `equals` then the `HashMap` will simply not work. The order of entries is the least of problems. – Brett Okken Aug 02 '14 at 13:22
  • Yes, you should get the same order, but don't expect to easily understand this order. – Pshemo Aug 02 '14 at 13:22
  • I do not believe this is a good answer for someone who would ask the question. This clearly depends upon the internal workings of library code. Pshemo is clearly experienced enough to know when shortcuts can be taken. – Tony Ennis Aug 02 '14 at 13:35
  • @BrettOkken You are right, but I treat this question as more like theoretical one (like on exam) rather than real world example, so I focused only on order part. Anyway I edited my answer and [asked OP for clarification of this problem](http://stackoverflow.com/questions/25095363/should-two-hashmap-store-in-the-same-order-if-i-am-inserting-the-same-thing-at-t/25095429#comment39049970_25095363). – Pshemo Aug 02 '14 at 13:48
0

You cannot make assumptions about the algorithm under the hood, so no. Further, you must not depend upon the underlying algorithm remaining the same over time. If you need the order to be the same, you must explicitly make it so. For example, instead of a HashMap, use a TreeMap and create an appropriate key or sort method.

Tony Ennis
  • 10,958
  • 6
  • 46
  • 68