• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LinkedHashSet and LinkedHashMap

 
Sharmila Punde
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Can someone please explain following -


Prints [1, 2, 4, 3] [1243] [1234] . Because PriorityQueue does not reflect ordering in iterator and toString. Ordering is reflected only by peek,poll,remove methods.

My question is , do the LinkedHashSet and LinkedHashMap reflect ordering in toString() method?

Regards
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LinkedHashSet and LinkedHashMap both are ordered collections (they maintain insertion order). So their iterators iterate over their elements in insertion order. Both of these classes inherit the toString method from AbstractCollection class. The documentation of the toString method in AbstractCollection class says
The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]").

So the toString method for both of them will reflect the ordering...
 
Sharmila Punde
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit,
It is very neat and nice explaination.
Thank you very much again.
Regards
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Sharmila,

Prints [1, 2, 4, 3] [1243] [1234] . Because PriorityQueue does not reflect ordering in iterator and toString. Ordering is reflected only by peek,poll,remove methods.


PriorityQueue reflects the natural ordering or by the order mentioned by given comparator in its functions, iterator and toString method. As even your code is mentioning the same. you just put not by mistake in the above quote.

 
Arunava Sircar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Salil,

I think what you have mentioned is wrong and what Sharmila has written is correct. PriorityQueue will never reflect the order if the elements are printed using the iterator or toString. The order is determined by natural ordering or by a Comparator and can be seen by the peek(), poll(), remove() methods.

--
Arunava
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic