Under certain situations, I need to evict the oldest element in a Java Set. The set is implemented using a LinkedHashSet, which makes this simple: just get rid of t
Set
LinkedHashSet is a wrapper for LinkedHashMap which supports a simple "remove oldest" policy. To use it as a Set you can do
Set set = Collections.newSetFromMap(new LinkedHashMap(){ protected boolean removeEldestEntry(Map.Entry eldest) { return size() > MAX_ENTRIES; } });