Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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();
        }
    }
}