# Java: truncated TreeSet

**URL:** <https://boards.straightdope.com/t/java-truncated-treeset/561316>\
**Category:** Factual Questions\
**Created:** [November 20, 2010, 7:38am UTC](https://boards.straightdope.com/t/java-truncated-treeset/561316 "2010-11-20T07:38:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lobot](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Lobot](https://boards.straightdope.com/u/Lobot)\
**Post date:** [November 20, 2010, 7:38am UTC](https://boards.straightdope.com/t/java-truncated-treeset/561316/1 "2010-11-20T07:38:40Z")

</div>

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:

```auto

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.

---

<div class="post-metadata">

**Author:** ![Sofis](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@Sofis](https://boards.straightdope.com/u/Sofis)\
**Post date:** [November 20, 2010, 7:59am UTC](https://boards.straightdope.com/t/java-truncated-treeset/561316/2 "2010-11-20T07:59:16Z")

</div>

Does foo contain a lot of duplicates?

---

<div class="post-metadata">

**Author:** ![Lobot](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Lobot](https://boards.straightdope.com/u/Lobot)\
**Post date:** [November 20, 2010, 8:02am UTC](https://boards.straightdope.com/t/java-truncated-treeset/561316/3 "2010-11-20T08:02:34Z")

</div>

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

Thank you.

---

<div class="post-metadata">

**Author:** ![Lobot](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Lobot](https://boards.straightdope.com/u/Lobot)\
**Post date:** [November 20, 2010, 8:05am UTC](https://boards.straightdope.com/t/java-truncated-treeset/561316/4 "2010-11-20T08:05:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sofis](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@Sofis](https://boards.straightdope.com/u/Sofis)\
**Post date:** [November 20, 2010, 8:07am UTC](https://boards.straightdope.com/t/java-truncated-treeset/561316/5 "2010-11-20T08:07:55Z")

</div>

No problem. 🙂

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.

---

<div class="post-metadata">

**Author:** ![Deeg](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/deeg/32/2955_2.png) [@Deeg](https://boards.straightdope.com/u/Deeg)\
**Post date:** [November 21, 2010, 4:32am UTC](https://boards.straightdope.com/t/java-truncated-treeset/561316/6 "2010-11-21T04:32:17Z")

</div>

If you’re looking for a tree map that can have multiple objects for one key, check out Google’s TreeMultimap at [http://guava-libraries.googlecode.com/svn/trunk/javadoc/index.html](http://guava-libraries.googlecode.com/svn/trunk/javadoc/index.html) . However, you can still only have one key-value pair (but you can store multiple values for the same key).
