Java 如何在番石榴表值中按排序顺序插入值

Java 如何在番石榴表值中按排序顺序插入值,java,collections,gson,guava,Java,Collections,Gson,Guava,我一直在尝试在Guava的表结构中按排序顺序插入一些值,下面是我的代码 NodeDetails node1 = new NodeDetails(); NodeDetails node2 = new NodeDetails(); NodeDetails node3 = new NodeDetails(); node1.setCLOUDTYPE("aws"); node1.setNODENAME("node1"); node2.setCLOUDTYPE("cloudstack"); node2.

我一直在尝试在Guava的表结构中按排序顺序插入一些值,下面是我的代码

NodeDetails node1 = new NodeDetails();
NodeDetails node2 = new NodeDetails();
NodeDetails node3 = new NodeDetails();

node1.setCLOUDTYPE("aws");
node1.setNODENAME("node1");

node2.setCLOUDTYPE("cloudstack");
node2.setNODENAME("node2");

node3.setCLOUDTYPE("azure");
node3.setNODENAME("node3");

Table<String, String, Object> table = TreeBasedTable.create();
    table.put("vmstat_usage", "cpu_idle", node1);
    table.put("vmstat_usage", "cpu_idle", node2);
    table.put("vmstat_usage", "cpu_idle", node3);
    table.put("vmstat_usage_percent", "cpu_system", node3);

Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting()
                    .create();
System.out.println(gson.toJson(table.rowMap()));
我浏览了这个链接,发现“table.rowMap返回一个SortedMap>。因此映射是按其行键排序的,但映射值是未排序的映射”
正如这里所建议的,我可以使用定义的comparator创建一个新的树表,并将我以前的表放在那里,或者在创建表的同时创建新的顺序

我试过这样的东西,但不起作用

Table<String, String, NodeDetails> table2 = TreeBasedTable.create(Ordering.natural(), Ordering.natural());
table2.putAll(table);
Table table2=TreeBasedTable.create(Ordering.natural(),Ordering.natural());
表2.putAll(表);
请给我举个例子说明如何做到这一点

Table<String, String, NodeDetails> table2 = TreeBasedTable.create(Ordering.natural(), Ordering.natural());
table2.putAll(table);