[flaptor-util] r207 committed - added sortTopN

0 views
Skip to first unread message

codesite...@google.com

unread,
Apr 16, 2010, 7:29:12 PM4/16/10
to flaptor-o...@googlegroups.com
Revision: 207
Author: san...@santip.com.ar
Date: Fri Apr 16 16:28:17 2010
Log: added sortTopN
http://code.google.com/p/flaptor-util/source/detail?r=207

Modified:
/trunk/src/com/flaptor/util/CollectionsUtil.java

=======================================
--- /trunk/src/com/flaptor/util/CollectionsUtil.java Mon Jul 6 14:52:40
2009
+++ /trunk/src/com/flaptor/util/CollectionsUtil.java Fri Apr 16 16:28:17
2010
@@ -23,6 +23,8 @@
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
+import java.util.SortedSet;
+import java.util.TreeSet;
import java.util.Map.Entry;

import com.google.common.base.Function;
@@ -34,6 +36,7 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;

/**
* Utility methods for Collections API
@@ -438,6 +441,19 @@
return floats;
}

-
+ public static <T extends Comparable<T>> SortedSet<T>
sortTopN(Iterable<T> iterable, int n) {
+ return sortTopN(iterable, n, Comparators.<T>naturalOrder());
+
+ }
+ 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;
+ }

}

--
You received this message because you are subscribed to the Google Groups "flaptor-open-source" group.
To post to this group, send email to flaptor-o...@googlegroups.com.
To unsubscribe from this group, send email to flaptor-open-so...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/flaptor-open-source?hl=en.

Reply all
Reply to author
Forward
0 new messages