How I can prove by jUnit test that HashSet handled collision. For example I can fill HashSet with 10000 elements in for loop, but which parameter should show me that I have collision, I suppose to think about collection size, but not pretty sure about it.
2 Answers
If you want to assert that HashSet handles collision of equal values, put in 2 equal values, and assert that only one item is in the set after:
HashSet<String> set = new HashSet<>(Arrays.asList("A", "A")); assertEquals(1, set.size()); If you want to assert that HashSet handles collision of equal hash codes, put in unequal values with the same hash code, and assert that there are two items in the set after:
assertEquals("Aa".hashCode(), "BB".hashCode()); HashSet<String> set = new HashSet<>(Arrays.asList("Aa", "BB")); assertEquals(2, set.size()); Comments
I don't understand what is the purpose of this test, but you can do it like this:
Set<String> mySet = new HashSet<>(); int numberOfRandomElements = 10000; mySet.addAll(createRandomElements(numberOfRandomElements)); int diff = numberOfRandomElements - mySet.size(); System.out.println(String.format("Number of elements removed: %d", diff)); 4 Comments
Vova Adamenko
Thanks Rafael. Maybe I will asked dummy question, but why size is diminished?
Rafael Senna
@VovaAdamenko HashSet store elements using its hashCode. When we try to add a element with same hashCode from another that already exists, the first is overrided.
Rafael Senna
I said that I dont understand the purporse of this test because: 1) As @Joeblade said, junit with random is not a good idea. Thats why I didnt make asserts in my response. 2) Why you are trying to test HashSet implementation? I'm sure it has been very well tested and works well
Vova Adamenko
Actually, I suppose to clarify my question. I need to count number of elements in HashSet with same hashCode, to see how many collision can exist in this HashSet, and as a result, to improve hasCode computing mechanism. Something like this. And finally I can't understand how to write this Test, I mean to count same HashCodes
HashSetalready did. You should focus on testing your code.