Java: truncated TreeSet

So I’m in the process of sorting a HashMap by its values into a LinkedHashMap, and one of the intermediate steps is as follows:

List foo = new ArrayList(hm.values());

TreeSet bar = new TreeSet(foo);

Now, foo has a size of over 4000, but bar has a size of only 99. Is this some sort of memory allocation problem? What’s going on?

Any help would be greatly appreciated.

Does foo contain a lot of duplicates?

I can’t believe how obvious that is. As in, blindingly obvious. Maybe that’s why I missed it. :slight_smile:

Thank you.

I take it, then, that TreeSets can’t contain duplicates?

No problem. :slight_smile:

And yes, sets in general cannot contain duplicates. I’m not intimately familiar with the Java libraries, so I’m not sure what class would suit you better.

If you’re looking for a tree map that can have multiple objects for one key, check out Google’s TreeMultimap at . However, you can still only have one key-value pair (but you can store multiple values for the same key).