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


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


1 Answer

New Answer


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.