I want to override LinkedHashMap's removeEldestEntry(Map.Entry) method to remove stale mappings automatically. Also I want to cleanup the entry that would be removed.
The entry is an AutoCloseable object. Can I call close() on it if it's going to be removed? Is it the best practice to do it as below?
public boolean removeEldestEntry(@Nonnull final Map.Entry<myKey, myObject> eldestEntry) {
if (size() > 100) {
eldestEntry.getValue().close();
return true;
}
return false;
}
If you are no longer using any of the underlying resources and have no need for the item, I do not see why not.