I started learning Java. When would I use a HashMap over a TreeMap?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
TreeMapis an example of aSortedMap, which means that the order of the keys can be sorted, and when iterating over the keys, you can expect that they will be in order.HashMapon the other hand, makes no such guarantee. Therefore, when iterating over the keys of aHashMap, you can’t be sure what order they will be in.HashMapwill be more efficient in general, so use it whenever you don’t care about the order of the keys.