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