Remove TreeMap entries less than x

dictionary, java, treemap

Solution

The most efficient way is probably storing this as a `SortedMap` and then using the one line

map.headMap(monDate).clear();

Problem

I have the following TreeMap: ``` private Map<Date,WeekSchedule> weeks = new TreeMap<Date,WeekSchedule>(); ``` And I need to remove all entries, whose date is before a given value. What is the most efficient way to do this? This is what I have so far: ``` public void removeWeeksBefore(Date monDate){ for (Map.Entry<Date, WeekSchedule> entry : weeks.entrySet()){ if(entry.getKey().before(monDate)){ weeks.remove(entry.getKey()); //This will destroy the iterator }else{ return; } } } ```

Original source