Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Collection;

import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * Modify the specified Collection so that only the first <code>limit</code> elements
     * remain, as determined by iteration order. If the Collection is smaller than limit,
     * it is unmodified.
     */
    public static void limit(Collection<?> col, int limit) {
        int size = col.size();
        if (size > limit) {
            if (col instanceof List<?>) {
                ((List<?>) col).subList(limit, size).clear();

            } else {
                Iterator<?> itr = col.iterator();
                for (int ii = 0; ii < limit; ii++) {
                    itr.next();
                }
                while (itr.hasNext()) {
                    itr.next();
                    itr.remove();
                }
            }
        }
    }
}