Insertion order in CopyOnWriteArraySet VS HashSet

291 Views Asked by At

Everyone knows HashSet stores elements in buckets based on the size of the hashtable and the elements' hash code values.

But how does CopyOnWriteArraySet store elements? I thought it makes a snapshot of those buckets and copies them. Looks like it doesn't. Does it store them in 'normal' array 1 by 1 and checks equals()? Does it even use hashing principle?

1

There are 1 best solutions below

6
khelwood On

CopyOnWriteArraySet is a Set-wrapper for CopyOnWriteArrayList, which stores its elements in an array, so it does not use hashing. That's why it doesn't have the O(1) lookup benefit of a HashSet.

The docs say it is only suitable for small sets.