LocalTaskImpl (String key, LocalTaskImpl task) { LocalTaskImpl result = super.put(key, task); if (size() > myConfig.taskHistoryLength) { ArrayList<Map.Entry<String, LocalTaskImpl>> list = new ArrayList<>(entrySet()); list.sort((o1, o2) -> TASK_UPDATE_COMPARATOR.compare(o2.getValue(), o1.getValue())); for (Map.Entry<String, LocalTaskImpl> oldest : list) { LocalTask value = oldest.getValue(); if (!value.isDefault() && value.isClosed()) { remove(oldest.getKey()); break; } } } return result; }