Assuming we had this class
final class Foo {
private final Set<String> bar = new HashSet<>();
public Foo() {
bar.add("one");
bar.add("two");
bar.add("three");
}
public boolean contains(final String s) {
return bar.contains(s);
}
}
Would it be threadsafe to instantiate Foo
and call contains
of this object from multiple threads?
- The reference to the collection is
private
andfinal
. No one can access directly the collection. - The only write access occurs in the constructor
- After the constructor is executed, the collection will only read and not modified.
If not, is there an pure Java alternative to Guava's immutable collections?