HashMap vs TreeMap | SoloLearn: Learn to code for FREE!

+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

7/15/2018 11:20:19 PM

Bercik97

1 Answer

New Answer

+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.