使用表guava进行hashbasedTable

时间:2012-07-27 20:53:41

标签: java hash hashmap guava

我计划将表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>();

但是这让我错了,我只想知道两件事

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

3 个答案:

答案 0 :(得分:26)

番石榴贡献者。

  1. 不要使用构造函数,请使用HashBasedTable.create()工厂方法。 (没有参数,或expectedRowsexpectedCellsPerRow。)
  2. 使用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());
        }
    }
}