HashSet Collisions in Java

前端 未结 2 587
陌清茗
陌清茗 2020-12-20 15:32

I have a program for my Java class where I want to use hashSets to compare a directory of text documents. Essentially, my plan is to create a hashSet of strings for each pa

2条回答
  •  甜味超标
    2020-12-20 15:46

    I think you did not ask for hash collisions, right? The question is what happens when HashSet a and HashSet b are added into a single set e.g. by a.addAll(b).

    The answer is a will contain all elements and no duplicates. In case of Strings this means you can count the number of equal String from the sets with a.size() before add - a.size() after add + b.size().

    It does not even matter if some of the Strings have the same hash code but are not equal.

提交回复
热议问题