NandaKumar
NandaKumar

Reputation: 905

Using table guava for hashbasedTable

I am planning to use table guava for a 3D hash map implementation. I downloaded that and I am able to import the files. My requirement is the below

I have the below file in my hand and I just have to aggregate the file accordingly and that is shown in the next step.

A100|B100|3
A100|C100|2
A100|B100|5

The aggregation part would be below

A100|B100|8
A100|C100|2

I tried using the below

Table<String,String,Integer> twoDimensionalFileMap= new HashBasedTable<String,String,Integer>();

But this throws me an error, I just want to know two things

  1. I just want to know, the arguments to be passed in the constructor of the HashBasedTable<String,String,Integer>()
  2. How to initialize the row,column and the value for this table just like we do it for the map it is map.put(key,value). In the similar sense could you guys tell me how to insert the values for this table?

Upvotes: 5

Views: 24145

Answers (3)

Francisco Spaeth
Francisco Spaeth

Reputation: 23903

From documentation:

Interface Table

Type Parameters:

R - the type of the table row keys
C - the type of the table column keys
V - the type of the mapped values

Your declaration is right. In order to use it, should be easy as:

Table<String,String,Integer> table = HashBasedTable.create();
table.put("r1","c1",20);
System.out.println(table.get("r1","c1"));

Upvotes: 5

ctesniere
ctesniere

Reputation: 131

Example of using : 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());
        }
    }
}

Upvotes: 2

Louis Wasserman
Louis Wasserman

Reputation: 198033

Guava contributor here.

  1. Don't use the constructor, use the HashBasedTable.create() factory method. (With no arguments, or with expectedRows and expectedCellsPerRow.)
  2. Use table.put("A100", "B100", 5), just like a Map except with two keys.

Upvotes: 28

Related Questions