【发布时间】:2017-05-11 20:08:30
【问题描述】:
我有一个表和一个 hashmap,我想删除(消除)guava 表中不在键 hashmap 中的列
示例
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}
结果
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}}
我的代码
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);
}
}
但这没有用
【问题讨论】:
-
你是什么意思,“它没有工作”?发生了什么?你试过写
table.columnKeySet().retainAll(hm.keySet())吗? -
@LouisWasserman 它给了我例外,我尝试
table.columnKeySet().retainAll(hm.keySet())效果很好,非常感谢