Forums Register Login

HashMap and ConcurrentHashMap

+Pie Number of slices to send: Send
Hi , Guys Good Day to all

I know HashMap and ConcurrentHashMap Classes which implements Map Interface. and HashMap is non-synchronized and ConcurrentHashMap is synchronised to lock portion.



Output : 2=310
1=1000
3=1788



and

Output : 1=1000
2=310
3=1788



Why?
+Pie Number of slices to send: Send
 

Shahir Deo wrote:Why?


What is your question?

Why the output is in a different order? That is because most Map implementations, including HashMap and ConcurrentHashMap, do not store elements in any specific order - so there's no reason to expect the elements to be printed in any particular order.
+Pie Number of slices to send: Send
The Output of ConcurrentHashMap is Sorted(or in Order) . but HashMap is not.
+Pie Number of slices to send: Send
 

Shahir Deo wrote:The Output of ConcurrentHashMap is Sorted(or in Order) . but HashMap is not.


It's the other way around.

But that's a coincidence. HashMap and ConcurrentHashMap do not make any guarantees about in which order they return their elements when you iterate over them.
+Pie Number of slices to send: Send
 

The Output of ConcurrentHashMap is Sorted(or in Order) . but HashMap is not.


As well as Jesper's comments, think about your sample size. 3 items printing out in order doesn't even give a reasonable indication as to whether collection is ordered or not.
+Pie Number of slices to send: Send

Output:
ConcurrentHashMap:

5=138
7=3101
8=310
1=1000
3=1788
4=1000




HashMap:

1=1000
3=1788
4=1000
5=138
7=3101
8=310

 
+Pie Number of slices to send: Send
Great. But you should still consider this to be a coincidence, because HashMap does not guarantee that it will always iterate over its elements in any particular order.

Try the following, for example:

Output:


b=237
c=221
a=349


Not in the order a, b, c!
+Pie Number of slices to send: Send



As I said a small sample doesn't give an indication as to whether it is ordered or not and especially when using low value Integers as keys given the way a HashMap works.
1
+Pie Number of slices to send: Send
Just to re-iterate what Tony has said, you can easily be tricked into believing HashMap is ordered (when the docs clearly tell you it isn't) when the sample size is small, and the range of values is small. Once the range of values gets distributed wide enough (either because there are lots of values or because the few values you have aren't closely ordered) then you can see there is no ordering. Tony showed an example with a suitably large data set. Here is an example of a small dataset with just 8 value, but those values are distributed over a large range:
Just the other day, I was thinking ... about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1054 times.
Similar Threads
remove the element in list while iterating through it.
Retrieving items from a HashSet
HashMap dilemma
i am able to insert a duplicate key in a map implementation. Not able to understand why?
Enhanced for loop and Iterator question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:25:26.