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.

3 Answers 3

8

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

Sign up to request clarification or add additional context in comments.

2 Comments

I wish I had seen that before!
The image is good but lacks the explanation of LinkedList and Deque class implementations, but is good.
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:

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.