Using a guava table for hashbasedTable

I plan to use the guava table to implement a 3D hash map. I downloaded this and I can import files. My requirement is below

I have the following file in my hand, and I just need to fill out the file accordingly, and this will be shown in the next step.

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

The aggregation part will be lower

 A100|B100|8 A100|C100|2 

I tried using below

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

But it throws me a mistake, I just want to know two things

  • I just want to know the arguments that should be passed in the constructor HashBasedTable<String,String,Integer>()
  • How to initialize the row, column and value for this table in the same way as we do it for the map is map.put(key,value) . In the same sense, could you tell me how to insert values ​​for this table?
+4
source share
3 answers

Guava is here.

  • Do not use the constructor, use the HashBasedTable.create() factory method. (With no arguments, or with expectedRows and expectedCellsPerRow .)
  • Use table.put("A100", "B100", 5) , like Map , except for two keys.
+26
source

From the 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 expression is correct. To use it should be easy:

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

Usage example: 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()); } } } 
+2
source

All Articles