我用 TreeMultiset 尝试了以下代码。似乎“*Twin”条目被丢弃,重复次数存储在 TreeMultiset 中。我想这是一个功能,而不是一个错误。是否有一个对象存储每个对象而不是一个键和重复次数?
public class Guava {
public static class Obj implements Comparable<Obj> {
String name;
int age;
public Obj(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return String.format("%s@%d", name, age);
}
@Override
public int compareTo(Obj o) {
return Integer.compare(age, o.age);
}
}
public static void main(String[] args) {
TreeMultiset<Obj> tree = TreeMultiset.create();
tree.add(new Obj("Ajo", 37));
tree.add(new Obj("AjoTwin", 37));
tree.add(new Obj("Ari", 31));
tree.add(new Obj("AriTwin", 31));
tree.add(new Obj("Fly", 1));
System.out.println(tree.size());
for (Obj obj : tree) {
System.out.println(obj);
}
}
}