is there a 3 dimensional Map/Table

2,315 views
Skip to first unread message

vamsi....@gmail.com

unread,
Jul 15, 2015, 10:43:39 PM7/15/15
to guava-...@googlegroups.com
Hi,

I've recently started using new collection types from Guava library.
Is there a 3 dimensional Map/Table similar to 2 dimensional com.google.common.collect.Table?
If not, is there any better way to achieve it other than using a HashMap inside a HashBasedTable or a HashBasedTable inside a HashMap?

Thanks,
Vamsi Attluri

Olivier Grégoire

unread,
Jul 16, 2015, 2:18:12 AM7/16/15
to vamsi....@gmail.com, guava-...@googlegroups.com

You can use a single HashMap<Key<A,B,C>, Value> where Key is a immutable class that contains the three keys you want to use. Make sure to properly overwrite its equals and hashCode methods.

public class Key<A,B,C> {
  private final A a;
  private final B b;
  private final C c;
  public Key (A a, B b, C c) {
    this.a = a;
    this.b = b;
    this.c = c;
  }
  public boolean equals (Object o) {
    if (obj instanceof Key) {
      Key<?,?,?> k = (Key<?,?,?>)obj;
      return Objects.equal(a, k.a) && Objects.equal(b, k.b) && Objects.equal(c, k.c);
    }
    return false;
  }

  public int hashCode() {
    return Objects.hashCode(a,b,c);
  }
}


--
guava-...@googlegroups.com
Project site: https://github.com/google/guava
This group: http://groups.google.com/group/guava-discuss
 
This list is for general discussion.
To report an issue: https://github.com/google/guava/issues/new
To get help: http://stackoverflow.com/questions/ask?tags=guava
---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/guava-discuss/7059615b-3d2d-442e-a5d8-5b0ff3f03070%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pete Gillin

unread,
Jul 16, 2015, 6:28:22 AM7/16/15
to Olivier Grégoire, vamsi....@gmail.com, guava-...@googlegroups.com
No, there isn't a 3-dimensional equivalent of Table.

If you just want cell-level access (e.g. look up a particular cell) then Olivier is correct and you can use a Map with a compound key. For bonus points, use autovalue to implement that compound key class and get the equals and hashCode methods for free.

Table also provides efficient row-level access (e.g. iterate over all the values in a row, or count them). If you want the equivalent of that, I think you're correct and you'll have to nest a Map in a Table or a Table in a Map (or a Map in a Map in a Map) depending on your exact requirements.

Finally, Table also provides a less-efficient (but convenient in terms of API) column-level access. If you want the equivalent of both row- and column-level access, I think you have a bit more work to do.

Pete.

Reply all
Reply to author
Forward
0 new messages