I want to be able to add specific words from a text into a vector. Now the problem is I want to avoid adding duplicate strings. The first thing that comes to my mind is to compare all strings before adding them, as the amount of entries grow, this becomes really inefficient solution. The only "time efficient" solution that I can think of is unordered_multimap
container that has included in C++11. I couldn't find a Java equivalent of it. I was thinking to add strings to the map and at the end just copying all entries to the vector, in that way it would be a lot more efficient than the first solution. Now I wonder whether there is any Java library that does what I want? If not is there any C++ unordered_multimap container equivalent in Java that I couldn't find?

- 3,686
- 21
- 64
- 103
-
3Have you considered using a Set? – Juned Ahsan Aug 28 '13 at 14:26
-
1As mentioned in the answers, avoid using `Vector`; it's heavily synchronized and thus slow. Prefer `List` instead, or in your case, just `Set`. – chrylis -cautiouslyoptimistic- Aug 28 '13 at 14:30
5 Answers
You can use a Set<String>
Collection. It does not allow duplicates. You can choose then as implementantion:
1) HashSet
if you do not care about the order of elements (Strings).
2) LinkedHashSet
if you want to keep the elements in the inserting order.
3) TreeSet
if you want the elements to be sorted.
For example:
Set<String> mySet = new TreeSet<String>();
mySet.add("a_String");
...
Vector
is "old-fashioned" in Java. You had better avoid it.

- 8,932
- 28
- 106
- 166
-
Do not recommend usage of `Vector` class anymore: [`Why is Java Vector class considered obsolete or deprecated?`](http://stackoverflow.com/q/1386275/1065197) – Luiggi Mendoza Aug 28 '13 at 14:31
-
2I didn 't recommend it. On the contrary I dissuaded him from using it. – arjacsoh Aug 28 '13 at 14:36
-
-
1You can read http://docs.oracle.com/javase/7/docs/api/java/util/Set.html to learn what each implementantion of Set is supposed to do. – arjacsoh Aug 28 '13 at 14:52
-
Please also add the usage of LinkedHashSet so that this answer would be a complete one :) – Sarp Kaya Aug 28 '13 at 14:57
-
@SarpKaya it should be something like `Set
mySet = new LinkedHashSet<>();` Since java 1.7 you don't need to explicit the type in the RHS. – Xam Apr 14 '18 at 20:20
You can use a set (java.util.Set):
Set<String> i_dont_allow_duplicates = new HashSet<String>();
i_dont_allow_duplicates.add(my_string);
i_dont_allow_duplicates.add(my_string); // wont add 'my_string' this time.
HashSet
will do the job most effeciently and if you want to keep insertion order then you can use LinkedHashSet
.

- 4,232
- 10
- 33
- 59
Use a Set
. A HashSet
will do fine if you do not need to preserve order. A LinkedHashSet
works if you need that.

- 6,588
- 25
- 31
You should consider using a Set:
A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.
HashSet should be good for your use:
HashSet class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.
So simply define a Set like this and use it appropriately:
Set<String> myStringSet = new HashSet<String>();

- 67,789
- 12
- 98
- 136
Set<String> set = new HashSet<String>();
The general contract of hashCode is:
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified.
This integer need not remain consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

- 736
- 6
- 15