Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single Element Java Data Structures that overwrite

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.

like image 713
Eduardo Avatar asked Nov 23 '25 08:11

Eduardo


1 Answers

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

like image 156
Jiri Kremser Avatar answered Nov 25 '25 20:11

Jiri Kremser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!