我想有一个c.g.c.c.Multimap仅基于键排序的.不应对值进行排序.我试图用番石榴来构建一些东西TreeMultimap,但我不能使用它,因为值类型没有实现Comparable.
public class MyObject /* doesn't implement Comparable */ {
private String name;
private int score;
// Getters/setters are implemented
public static Function<MyObject,Integer> myObjectToScore {
@Override public Integer apply (MyObject o) { return o.score; }
}
public static Multimap<Integer,MyObject> indexOnScore(Iterable<MyObject> i) {
Multimap<Integer,MyObject> m = Multimaps.index(i, myObjectToScore());
// Do the sort of the keys.
return m;
}
}
Run Code Online (Sandbox Code Playgroud)
我已经考虑过获取一些SortedSet密钥,然后迭代排序集中的每个密钥以获取各种值,但我希望在Guava中使用现有的(但尚未发现的)功能而不是使用这种黑客.
注意:我不会制作MyObject工具,Comparable因为它对我的实际对象毫无意义.
输入/输出示例:
Set<MyObject> s = Sets.newHashSet(
new …Run Code Online (Sandbox Code Playgroud)