HashMap vs TreeMap

A

Ahmed Moustafa

What are the differences between HashMap and TreeMap? What is the
application of each implementation?

Thanks in advance!
 
A

Adam Maass

Christophe Vanfleteren said:
A TreeMap implements SortedMap, so when you iterate over the contents,
you'll get the content sorted by either their natural order, or by a
Comparator you definied when creating the TreeMap.

With the regular HashMap, iterating over the content can result in any order
(you won't get them in eg. the order you inserted them into the Map).

Insertion into a HashMap is, on average, much quicker than insertion into a
TreeMap containing as many elements.

If order is not important, use HashMap.

-- Adam Maass
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top