java - 按满足一定条件的值移除元素

标签 java dictionary data-structures

从这些数据结构中,我想按值删除满足特定条件的元素

<Data Structures>

 - RowSortedTable<String, String, Double> a;     (Guava Table)
 - HashMap<String, Double> b;

来自previous question ,我使用 Collections.Singleton 找到了优雅的答案,但是,似乎需要精确匹配。

hmap.values().removeAll(Collections.singleton("Two"));

在这里,我想从表或 map 中删除值小于特定阈值的元素。您将如何编写代码?


我刚查了两个答案,都是关于map的答案,那table case呢?我的解决方案如下。

for (Iterator<String> it1 = proptypeconf.columnKeySet().iterator(); it1.hasNext();) {
        String type = it1.next();
        System.out.println(type);
        for (Iterator<Map.Entry<String, Double>> it2 = proptypeconf.column(type).entrySet().iterator(); it2.hasNext();){
            Map.Entry<String, Double> e = it2.next();
            if (e.getValue() < conflist.get(index-1)) {
                it2.remove();
            }
        }
    }

最佳答案

Iterator<Integer> iterator = hmap.values().iterator();
while (iterator.hasNext()) {
  if (iterator.next() < threshold) {
     iterator.remove();
  }
}

当然,如果你使用的是 Java 8,那就简单多了:

hmap.values().removeIf(value -> value < threshold);

表格的工作方式完全相同;只需使用 table.values() 而不是 hmap.values()

关于java - 按满足一定条件的值移除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33134499/

相关文章:

Java应用程序,具有多个场景

java - 基于 Spring Java 的配置优于基于 XML 的配置?

scala - Scala 2.8.0 和 2.7.7 中 Map 行为的这种令人惊讶的差异(对我来说)是预期的吗?

python - 什么是不存储键的 Python 哈希表/字典实现?

java - 以编程方式获取公共(public) S3 对象的 URL(链接)

java - 将 2 月 28 日加 1 个月,结果应为 3 月 31 日

javascript - 使用 HTML5 放大国家/地区的世界地图

jquery - 谷歌 JavaScript map

ruby 性能 : Multi-key hashes

c++ - 排序 char* 数组