Iterating over a map and putting its values in java

396 Views Asked by At

I have created the data structure that is google based guava table and it is shown below

    final Table<String, String, List<String>> values = HashBasedTable.create();
    values.put("ton bon", "currency", Lists.newArrayList("ccdd", "rode1", "cwey", "Certy"));
    values.put("ton bon", "racy", Lists.newArrayList("wqadd", "werde", "ihtr", "ytre"));

Below is the way in which i am iterating over this collection

    Map<String, List<String>> fmap = new HashMap<String, List<String>>();
    List<String> validColumnKeys = Arrays.asList("ccdd", "rode1", "cwey", "Certy");
    Map<String, List<String>> row = values.row("ton bon");
    for (String columnKey : validColumnKeys) {
        fmap.put(columnKey, row.get(columnKey));
    }

Now i want to modify my condition in such a way that if the row key contains the value of ton bon and the column key contains these values "ccdd","rode1","cwey","Certy" then it should fill the map named fmap as shown below and similarly for the second condition too.

The map i want :

Key        Value 
ccdd        currency
rode1       currency
cwey        currency    
Certy       currency
wqadd       racy
werde       racy
ihtr        racy
ytre        racy

But unfortunately the code I have tried is not working. Please advise what went wrong and how can I overcome from this.

1

There are 1 best solutions below

7
On BEST ANSWER

It Should be a loop inside loop for column and row

final Table<String, String, List<String>> values = HashBasedTable.create();
values.put("ton bon", "currency", Lists.newArrayList("ccdd","rode1","cwey","Certy"));
values.put("ton bon", "racy", Lists.newArrayList("wqadd","werde","ihtr","ytre"));
Map<String, List<String>> row = values.row("ton bon");
Map<String, String> fmap = new HashMap<String, String>();
System.out.println("Key\tValue");
for(String columnKey:row.keySet()) {
    List<String> rowValues = row.get(columnKey);
    for(String rowValue:rowValues) {
        System.out.println(rowValue + "\t" + columnKey);
        fmap.put(rowValue, columnKey);
    }
}

will print

Key     Value
wqadd   racy
werde   racy
ihtr    racy
ytre    racy
ccdd    currency
rode1   currency
cwey    currency
Certy   currency

The values can be stored in fmap.put(rowValue, columnKey);.....Note: here the key should be unique

The optimized method is

final Table<String, String, List<String>> values = HashBasedTable.create();
values.put("ton bon", "currency", Lists.newArrayList("ccdd","rode1","cwey","Certy"));
values.put("ton bon", "racy", Lists.newArrayList("wqadd","werde","ihtr","ytre"));

Collection<Map<String,List<String>>> obj = Collections2.transform(Collections2.filter(Arrays.asList(new String[]{"ton bon"}), Predicates.in(values.rowMap().keySet()) ), Functions.forMap(values.rowMap()));
System.out.println("Key\tValue");
for(Map<String,List<String>> item: obj){
    for(String key: item.keySet()){
        List<String> items = item.get(key);
        for(String value: items){
            System.out.println(value + "\t" + key);
        }
    }
}