trim Map - Android java.util

Android examples for java.util:Map

Description

trim Map

Demo Code


//package com.book2s;

import java.util.Iterator;
import java.util.Map;

public class Main {
    public static void trimMap(Map map, int numberOfElements) {
        if (map.size() < numberOfElements) {
            return;
        }//  w  w  w.  ja  v a 2  s. c o  m

        numberOfElements = map.size() - numberOfElements;
        Iterator it = map.entrySet().iterator();
        int counter = 0;

        while (it.hasNext()) {
            if (counter <= numberOfElements) {
                it.next();
                counter++;
            }
            it.remove();
        }
    }
}

Related Tutorials