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>();

しかし、これはエラーをスローします。2つのことを知りたいだけです

  1. 知りたいのは、のコンストラクターで渡される引数ですHashBasedTable<String,String,Integer>()
  2. マップの場合と同じように、このテーブルの行、列、および値を初期化する方法はmap.put(key,value). 同様の意味で、このテーブルに値を挿入する方法を教えていただけますか?
4

3 に答える 3

28

グアバの寄稿者はこちら。

  1. コンストラクターを使用せず、HashBasedTable.create()ファクトリ メソッドを使用します。(引数なし、またはexpectedRowsand ありexpectedCellsPerRow。)
  2. 2 つのキーを除いて とtable.put("A100", "B100", 5)同じようにを使用します。Map
于 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 に答える