Originally Posted by
jashburn
Sorry, HashSet
cannot store duplicates. If it does, it is a major bug that breaks applications everywhere. Having said that, an object that is a "duplicate" of another implies the objects are "equal", and in this context "equality" is defined as the objects returning the same values as each other for the their implementation of the equals() and hashCode() methods.
To put this in another way, if object objA is a duplicate or is equal to object objB, then
- objA.equals(objB) and objB.equals(objA) are true, and
- (objA.hashCode() == objB.hashCode()) is true
In this case you can only store either objA or objB in a HashSet, and not both.
If you were referring to a post such as
http://www.javaprogrammingforums.com...ted-value.html where a HashSet stores duplicate elements, then be aware that it's an example of not properly overriding the equals() and hashCode() methods. See
http://www.javaprogrammingforums.com...-use-sets.html for a further write-up on this.
If the above doesn't help, and/or you've come across a case where duplicated
Strings are stored in a HashSet, then I'm afraid I'll need to see some example code to further understand the problem.
Well, first of all, thank you for your quick responses. The link, How to Use Sets, was a helpful link in furthering my understanding of possible problems in the future. However, I solved my own problem by just adding an if statement that does checks if the string equals itself and if it does, it creates a new 'replaceAll(regex, replacement) string and then I store the new string. Regex being the value of my pre-converted string and the replacement being the pre-converted string variable.
So the code(small snippet) looked something like this:
Original:
int chunkx = chunkCoords.getX(); //get chunk location of said chunk
int chunkz = chunkCoords.getZ();
String coordSet = (chunkx + "," + chunkz); //store set of coords as string
Collection.add(coordSet);
Collection.add(coordSet);
count++;
}
New:
int chunkx = chunkCoords.getX(); //get chunk location of said chunk
int chunkz = chunkCoords.getZ();
String cx = new Integer(chunkx).toString();
String cz = new Integer(chunkz).toString();
String coordSet = (cx + "," + cz); //store set of coords as string
if(coordSet.equals(coordSet)){
String cxz = coordSet.replaceAll((cx + "," + cz), coordSet);
Collection.add(cxz);
count++;
}