Search code examples
javahashmapguava

table guava elimination columKey


I have a table and a hashmap, I want to delete(eliminate) columns in the guava table which are not in keys hashmap

example

table guava

{5667={20=10, 222=7, 547=10, 1590=10, 3802=10, 4383=3, 5680=10, 7987=9, 
      9181=10, 9325=2},
7021={20=8, 222=8, 547=9, 1590=10, 3802=3, 4383=1, 5680=9, 7987=9,
      9181=9, 9325=2}}

my HashMap hm
{20=0, 222=0, 3802=0, 4383=0, 7987=0, 9181=0, 9325=0}

Result

new Table table (same table)

{5667={20=10, 222=7, 3802=10, 4383=3,7987=9,9181=10, 9325=2},
7021={20=8, 222=8, 3802=3, 4383=1,7987=9, 9181=9, 9325=2}}

My code

   public class columnElimination {

   public static Table< Integer, Integer, Integer> doc(Table< Integer, 
   Integer, Integer> table, HashMap< Integer, Integer> hm) throws 
    ClassNotFoundException {

    table.columnMap().keySet().removeIf(key -> !hm.containsKey(key));
    System.out.println("new table");
    System.out.println(table);
    return (table);
}
}

but that didn't work


Solution

  • This should work, but you haven't explained what exceptions you got. In the meantime, I would write this more simply as table.columnKeySet().retainAll(hm.keySet()) anyway.