searching for keys when values are selected

63 views Asked by At

I am using google table to store the collection in this format as shown:

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Lists;
import com.google.common.collect.Table;

public class mapdemo {

    public static void main(String[] args) {

        final Table<String, String, List<String>> values = HashBasedTable
                .create();
        values.put("B1", "RtyName",
                Lists.newArrayList("weepn", "weepfnb", "eedgeft", "xbteehy"));
        values.put("B2", "EUOPName",
                Lists.newArrayList("aaa", "bbbb", "ccc", "dddd"));
        System.out.println(values.get("B1", "RtyName")); // prints the list
        System.out.println(values.get("B2", "XXXName")); // prints the list
    }
}

Now, it outputs:

[weepn, weepfnb, eedgeft, xbteehy]
[aaa, bbbb, ccc, dddd]

I want to customize this same program, such as lets say if any one of the value of the list, I select lets say from the below pair

"B1", "RtyName", Lists.newArrayList("weepn", "weepfnb", "eedgeft", "xbteehy"

I select the value weepn then it should print the RtyName and B1 similiarly let say i select the value aaa then it should print the XXXName and B2.

So, please advise how to achieve this as now i want to search for values.

1

There are 1 answers

0
Ajay On

There is no direct way to get keys on based of value So you can iterate on all cellSet(row) of a table and check each list if it contains value or not . If you are doing it for big map than you can think of using BiMap
//code to iterate

for(final Cell<String, String, List<String>> row : values.cellSet()){
           if(row.getValue().contains(val)){
               System.out.println(//desired output);
           }