Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.LinkedHashSet;

import java.util.Set;

public class Main {
    /**
     * @param elements
     * @param maxNumber
     * @return the maxNumber of elements in ids and rest are skipped
     */
    public static <T> Set<T> getElementsOfMaxSize(Set<T> elements, int maxNumber) {
        // The returned set should maintain the same order as the input elements
        Set<T> maxIds = new LinkedHashSet<T>();

        if (maxNumber <= 0) {
            return maxIds;
        }

        for (T id : elements) {
            maxIds.add(id);

            maxNumber--;

            if (maxNumber <= 0) {
                break;
            }

        }

        elements.removeAll(maxIds);

        return maxIds;
    }
}