5

我打算使用表番石榴来实现 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>();

但这会给我一个错误,我只想知道两件事

  1. 我只想知道,要在构造函数中传递的参数HashBasedTable<String,String,Integer>()
  2. 如何初始化该表的行、列和值,就像我们对它的映射所做的那样map.put(key,value)。在类似的意义上,你们能告诉我如何插入这个表的值吗?
4

3 回答 3

28

番石榴贡献者在这里。

  1. 不要使用构造函数,使用HashBasedTable.create()工厂方法。(不带参数,或带expectedRowsand expectedCellsPerRow。)
  2. 使用table.put("A100", "B100", 5),就像 a Mapexcept 有两个键一样。
于 2012-07-27T21:04:28.873 回答
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"));
于 2012-07-27T21:04:52.873 回答
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());
        }
    }
}
于 2014-10-15T14:09:51.967 回答