2

I've been using TreeMap<String,Object> to store objects as they don't allow entries with duplicate keys,

Is there a a similar data structure that only takes in a value/key and doesn't allow duplicate entries?

so something like Type<String> where all the Strings have to be different (or they overwrite)

until now ive just been using Vector<String> with checks to see if an entry is already in the vector before adding it which is a bit messy.

Eduardo
  • 6,900
  • 17
  • 77
  • 121

3 Answers3

9

Set is what you are looking for. There are couple of implementations of this interface. The most common is the HashSet (fast contains() operation but the order is not guaranteed), TreeSet (this is actually implementation of SortedSet, the instances stored in TreeSet should implement Comparable) and the last commonly used is the LinkedHashSet. The order of LinkedHashSet is derived from the time elements were added into the container.

Recently, I saw this handy diagram: diagram

Jiri Kremser
  • 12,471
  • 7
  • 45
  • 72
5

You're looking for a Set<String>, more specifically a TreeSet<String>.

Also, please don't use Vector, instead use List backed by an ArrayList. Refer to:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
3

You can use Set and the equivalent to what you propose is TreeSet. However I think HashSet will do better job for the task you describe.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176