我计划将表guava用于3D哈希映射实现。我下载了,我可以导入文件。我的要求如下
我手中有下面的文件,我只需要相应地聚合文件,然后在下一步中显示。
A100|B100|3
A100|C100|2
A100|B100|5
汇总部分将在
之下A100|B100|8
A100|C100|2
我尝试使用以下
Table<String,String,Integer> twoDimensionalFileMap= new HashBasedTable<String,String,Integer>();
但是这让我错了,我只想知道两件事
HashBasedTable<String,String,Integer>()
map.put(key,value)
所做的那样。在类似的意义上你们可以告诉我如何插入这个表的值吗?答案 0 :(得分:26)
番石榴贡献者。
HashBasedTable.create()
工厂方法。 (没有参数,或expectedRows
和expectedCellsPerRow
。)table.put("A100", "B100", 5)
,就像Map
一样,除了两把钥匙。答案 1 :(得分:5)
来自文档:
接口表
类型参数:
R - the type of the table row keys C - the type of the table column keys V - the type of the mapped values
你的声明是对的。为了使用它,应该很容易:
Table<String,String,Integer> table = HashBasedTable.create();
table.put("r1","c1",20);
System.out.println(table.get("r1","c1"));
答案 2 :(得分:2)
使用示例:http://www.leveluplunch.com/java/examples/guava-table-example/
@Test
public void guava_table_example () {
Random r = new Random(3000);
Table<Integer, String, Workout> table = HashBasedTable.create();
table.put(1, "Filthy 50", new Workout(r.nextLong()));
table.put(1, "Fran", new Workout(r.nextLong()));
table.put(1, "The Seven", new Workout(r.nextLong()));
table.put(1, "Murph", new Workout(r.nextLong()));
table.put(1, "The Ryan", new Workout(r.nextLong()));
table.put(1, "King Kong", new Workout(r.nextLong()));
table.put(2, "Filthy 50", new Workout(r.nextLong()));
table.put(2, "Fran", new Workout(r.nextLong()));
table.put(2, "The Seven", new Workout(r.nextLong()));
table.put(2, "Murph", new Workout(r.nextLong()));
table.put(2, "The Ryan", new Workout(r.nextLong()));
table.put(2, "King Kong", new Workout(r.nextLong()));
// for each row key
for (Integer key : table.rowKeySet()) {
logger.info("Person: " + key);
for (Entry<String, Workout> row : table.row(key).entrySet()) {
logger.info("Workout name: " + row.getKey() + " for elapsed time of " + row.getValue().getElapsedTime());
}
}
}