I need to ensure that a certain Set<String>
I create is not modified somewhere else in the code. Of course, I ended up using Guava's ImmutableSet for this.
This immutable set is quite large (approx. 59K Strings), and I have to perform a Set#contains
check every time a particular method is called. So I was wondering if there is any way of specifying the look-up in big set. Guava's documentation says:
A high-performance, immutable Set with reliable, user-specified iteration order. Does not permit null elements.
What does user-specified iteration
mean if the immutable set is created by calling ImmutableSet#copyOf(aHashSet)
? Will the performance of contains(String)
be adversely affected if I use ImmutableSet#contains
instead of HashSet#contains
? To be more precise, my question is the following:
With a decent hash function and not too many elements getting in the same bucket, one would expect HashSet#contains
to be O(1). Will an ImmutableSet created using copyOf
adhere to this?
There are two reasons behind my suspicion that this might not be the case:
Guava forum discussion on precisely this question (didn't seem to provide a conclusive answer though).
It's not clear to me whether
ImmutableSet#contains
defers tojava.util.Set#contains
(i.e., the implementation inHashSet
, in my case) orcom.google.common.collect.ImmutableCollection#contains
. If it is the latter, thenImmutableSet#contains
will be an O(n) operation.