Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Comparator;

import java.util.SortedSet;
import java.util.TreeSet;

import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;

public class Main {
    public static <T extends Comparable<T>> SortedSet<T> sortTopN(Iterable<T> iterable, int n) {
        return sortTopN(iterable, n, Ordering.<T>natural());
    }

    public static <T> SortedSet<T> sortTopN(Iterable<T> iterable, int n, Comparator<T> comparator) {
        TreeSet<T> r = Sets.newTreeSet(comparator);
        for (T t : iterable) {
            r.add(t);
            if (r.size() > n) {
                r.pollLast();
            }
        }
        return r;
    }
}