• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap and ConcurrentHashMap  RSS feed

 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Output of ConcurrentHashMap is Sorted(or in Order) . but HashMap is not.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shahir Deo
Ranch Hand
Posts: 80
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!