Search code examples
javaandroidhashtableguavaandroid-guava

Java | Get column keys from a row key


I have Table like Table<String, String, String> distributions = HashBasedTable.create();

|----------------------------------------------|
| Row             | Column   | Value           |
|----------------------------------------------|
| aaa             | a        | Hard Rock       |
| aaa             | n        | Art Pop         |
| abc             | b        | Surrealist Film |
| abc             | c        | Blockbuster     |
|----------------------------------------------|


My Column values are all unique but my Row values can be repeated.

so How can I get all the Column keys given a Row key?


Solution

  • You can use Table.rowMap() to get "a view that associates each row key with the corresponding map from column keys to values"; the view is a Map and its keySet() contains all of the Column keys for a given Row key:

     distributions.rowMap().get("aaa").keySet(); // [a, n]
     distributions.rowMap().get("abc").keySet(); // [b, c]