【问题标题】:table guava elimination columKey表番石榴消除columKey
【发布时间】: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()) 效果很好,非常感谢

标签: java hashmap guava


【解决方案1】:

应该工作,但你没有解释你有什么例外。同时,无论如何,我会更简单地写成table.columnKeySet().retainAll(hm.keySet())

【讨论】:

  • 他给我写信Exception in thread "main" java.lang.UnsupportedOperationException
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多