1

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?

2
  • 3
    Have you considered using a Set? Commented Aug 28, 2013 at 14:26
  • 1
    As mentioned in the answers, avoid using Vector; it's heavily synchronized and thus slow. Prefer List instead, or in your case, just Set. Commented Aug 28, 2013 at 14:30

5 Answers 5

13

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.

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

6 Comments

Do not recommend usage of Vector class anymore: Why is Java Vector class considered obsolete or deprecated?
I didn 't recommend it. On the contrary I dissuaded him from using it.
So what are the differences between sets?
You can read docs.oracle.com/javase/7/docs/api/java/util/Set.html to learn what each implementantion of Set is supposed to do.
Please also add the usage of LinkedHashSet so that this answer would be a complete one :)
|
3

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.

Comments

2

Use a Set. A HashSet will do fine if you do not need to preserve order. A LinkedHashSet works if you need that.

Comments

2

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>();

Comments

0
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.

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.