0

I want to store a set of integers with duplicates allowed, for example 1, 5, 3, 3, 4, 6.

If I use a HashSet, the duplicates are lost. A List feels wrong as the items have no order implied. I need to be able to delete items from the middle quickly which rules out ArrayList. LinkedList still implies the items have some order which I don't like.

Is there another data structure I should use for this? Or should I tell HashSet to compare Integer based on reference equality?

I know I can work around this in a few ways but I'm more interested in the most 'correct' solution.

Flash
  • 15,945
  • 13
  • 70
  • 98
  • The *definition* of `Set` is that it doesn't store duplicates (and even using reference equality with `Integer` wouldn't work; `Integer` tends to intern small values). What are you wanting to accomplish? – chrylis -cautiouslyoptimistic- Sep 02 '13 at 11:32
  • @chrylis Say I want to represent a bucket of tokens where each token has a number on it. Some of the tokens might have the same number but there is no order to the tokens in the bucket. What data structure is best to represent the bucket? – Flash Sep 02 '13 at 11:47
  • You want a "bag" or "multiset", which is basically a special-purpose `Map`. Guava, as suggested by Marcin, is a good choice. – chrylis -cautiouslyoptimistic- Sep 02 '13 at 12:20

4 Answers4

5

You might use Guava's multiset, if this dependency is not a problem. As a bonus, guava has many other useful general-purpose utilities.

Marcin Łoś
  • 3,226
  • 1
  • 19
  • 21
3

The LinkedList is the way to go. If you need it sorted, you can use Collections.sort();

Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45
0

Use a multiset-style collection. It is missing for some reason in Java libraries, so you'll want to either implement Collection directly yourself or use Google Guava's MultiSet.

A list is fine, and overhead is not bad. If you don't care about ordering, don't use it.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
0

List will work just fine, its the easiest solution for your problem without any major drawbacks and this is most of the times favorable to some elegant but complex ones.

Other than that see this answer Duplicate values in the Set collection?

Community
  • 1
  • 1
Patrick
  • 33,984
  • 10
  • 106
  • 126