Java tutorial
//package com.java2s; import java.util.Iterator; import java.util.Map; public class Main { public static void trimMap(Map map, int numberOfElements) { if (map.size() < numberOfElements) { return; } numberOfElements = map.size() - numberOfElements; Iterator it = map.entrySet().iterator(); int counter = 0; while (it.hasNext()) { if (counter <= numberOfElements) { it.next(); counter++; } it.remove(); } } }