5

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?
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
NandaKumar
  • 905
  • 4
  • 15
  • 19

3 Answers3

28

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.
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Thanks for the information guys... I did try this and it is working fine .... I am just curious to know if we will be able to sort this Table in descending value based on the value parameter .... Is there any method to do that ... If so could you ppl please share with me .. This will be really useful for me..,. Thanks – NandaKumar Jul 27 '12 at 21:15
  • 1
    IMO, if using a basic constructor fails, that a minor code smell, but I understand that sometimes that's the way things have to work. But, at a minimum, said constructor _should probably be made_ `protected` so naive users are forced to use the factory. – user949300 Jul 27 '12 at 21:53
  • 2
    @user949300 There's nothing wrong about the constructor being private . With a protected one you could `extend HashBasedTable` and your class could break if the implementation changes someday. As there seem to be no benefits in such a subclassing, it's better to prohibit it. Sure, making the class final would work too, but the factory (unlike the ctor) saves you typing the type parameters. – maaartinus Jul 28 '12 at 13:12
  • private is fine too. I was saying that the constructor should not be public if it doesn't really work in basic cases. – user949300 Jul 28 '12 at 16:46
  • It [isn't public here](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/HashBasedTable.html). What's the problem? – Louis Wasserman Jul 28 '12 at 16:49
  • @LouisWasserman this way, I can't do HashBasedTable.create() in a call to a function without specifying the parameter. e.g. in a loop – Dejell Oct 12 '14 at 16:56
  • 1
    @maaartinus *"but the factory saves you typing the type parameters"* - this is no longer valid reason since Java 7 – user11153 Feb 06 '15 at 13:06
  • @user11153 Sort of. The diamonds are a useless visual clutter, too. As I haven't seen (let alone written) a raw constructor call since years, Oracle could simply have saved the needless two characters. I never use diamonds as they're not worth it - they're big PITA in case I'll need to port something back to 1.6 (Android). – maaartinus Feb 06 '15 at 22:32
  • @maaartinus *"Oracle could simply have saved the needless two characters."* - I'm afraid that would break backward compatibility. BTW `new HashBasedTable<>()` would be shorter than `HashBasedTable.create()` – user11153 Feb 07 '15 at 13:19
  • @user11153 Mixing both 1.4 and 1.7 style in a single source file goes far beyond backward compatibility. A 1.7+ javac could assume the source is modern (unless instructed otherwise) and produce no warning, as there's nothing to warn about. Even better would be a way to mark the version of a source code in the source file itself. Concerning your BTW, agreed. – maaartinus Feb 07 '15 at 21:59
5

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"));
Francisco Spaeth
  • 23,493
  • 7
  • 67
  • 106
2

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());
        }
    }
}
ctesniere
  • 131
  • 8