Reputation: 2681
I have some data that's going to be coming in sorted order (first the entire set of keys in sorted order and then duplicates in random order). So, I could use both a LinkedHashMap or a TreeMap to preserve the ordering (right?). My question then is, which of them is faster and which is more space efficient?
Upvotes: 7
Views: 1597
Reputation:
LinkedHashMap is faster for insertion because it won´t have to unnecessarily compare values while inserting like TreeMap, as stated by @EJP. And since LinkedHashMap only needs a link for the previous and to the next key, while TreeMap needs a link to the parent node and 1+ links to children, I think that TreeMap will also consume a slightly bigger memory.
So my vote is for LinkedHashMap. Less memory, less time, and of course, less CPU .
Upvotes: 7