【问题标题】:Sorting Guava tables in descending order based on values根据值按降序对 Guava 表进行排序
【发布时间】:2012-07-27 21:33:42
【问题描述】:

我打算使用番石榴表来以表格式存储我的值。我想知道一些函数,它根据表中的值执行降序排序......你们能否对此发表一些看法。谢谢。

【问题讨论】:

标签: java hashmap guava


【解决方案1】:

就像使用地图一样,您应该复制cellSet(),按值对单元格进行排序,然后将其放入保持顺序的ImmutableTable

Ordering<Table.Cell<String, String, Integer>> comparator =
  new Ordering<Table.Cell<String, String, Integer>>() {
    public int compare(
        Table.Cell<String, String, Integer> cell1, 
        Table.Cell<String, String, Integer> cell2) {
      return cell1.getValue().compareTo(cell2.getValue());
  }
};
// That orders cells in increasing order of value, but we want decreasing order...
ImmutableTable.Builder<String, String, Integer>
    sortedBuilder = ImmutableTable.builder(); // preserves insertion order
for (Table.Cell<String, String, Integer> cell :
    comparator.reverse().sortedCopy(table.cellSet())) {
  sortedBuilder.put(cell);
}
return sortedBuilder.build();

无论如何,这或多或少正是您编写的按值对地图进行排序的代码。

【讨论】:

  • 谢谢你的回答。如果可能的话,你能不能看看这个或链接参考
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-12
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 2021-09-06
  • 2016-03-10
相关资源
最近更新 更多