UndefinedReference
UndefinedReference

Reputation: 1253

How to Delete Duplicate Values in a HashSet

I'm creating a HashSet of strings grabbed from a table in SQL database. So far it is working and the results are printing correctly.

However, I am curious as to how I can remove duplicate strings from my HashSet before I print it out.

List<String> resultsArray = new ArrayList<String>();
resultsArray.add(results.getString("SCINAME"));

Set<String> set = new HashSet<String>(resultsArray);
System.out.println(set);

Is there a quick solution for this?

Thanks.

Upvotes: 0

Views: 3473

Answers (1)

JNL
JNL

Reputation: 4703

I think, you are missing something here. In the question you mentioned HashMap and in the implementation you have HashSet.

Remember, HashSet implements the Set interface, which does not contain duplicates.

So, when you implement objectOfHashSet.add(duplicates); it will return false.

Upvotes: 6

Related Questions