+ 7

HashMap vs TreeMap

Hi, im kinda confused. People says that HashMap is unsorted and TreeMap is sorted. It works on Sets but it doesn't work on Maps. Can someone explain me why it not works? Thanks ! https://code.sololearn.com/c13X323OGuc5/#java

15th Jul 2018, 11:20 PM
Bercik97
Bercik97 - avatar
1 Odpowiedź
+ 6
Hello, Bercik97 ! "HashMap" does not give any guarantees as to the iteration order. It can (and will) even completely change when adding new items. "TreeMap" will iterate according to the "natural order" of the keys according to their compareTo () (or external Comparator) method.
16th Jul 2018, 5:01 AM
Alexander Sokolov
Alexander Sokolov - avatar