How can i use set to check duplication in an array of strings
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   kannan_Chandran
Posted On:   Wednesday, June 15, 2005 01:39 AM


How can i use set to check duplication in an array of strings



i want to check the string object for duplication how can i achieve it through Set in Collection


Please use html tags to format code blocks.

Re: How can i use set to check duplication in an array of strings

Posted By:   Eelco_Cramer  
Posted On:   Wednesday, June 15, 2005 03:06 AM

I don't know what it is that you are trying to archive? If you want to remove the duplicate strings you might want to use a java.util.Set instance instead of an array. A set does not allow duplicates.


Well to answer your question:


String [] myStrings = {"a", "b", "c", "b", "d"};
Set mySet = new java.util.TreeSet();

for (int i=0; i if (mySet.add(myStrings[i]) {
// myStrings[i] is not a duplicate
} else {
// myStrings[i] is a duplicate
}
}

// now you have a Set containing only unique strings.
About | Sitemap | Contact