I have the following method:
private static HashSet<Integer> ids = new HashSet<>();
public static void someMethod(SomeObject o) {
// some code
ids.add(o.getId());
if(ids.size() > 10) {
// do something
}
else {
// do something else
}
}
An easy way to make this method thread safe is to add the keyword synchronized
.
I was wondering if there is already some more appropriate way to add an item in a map/set and check size atomically