从哈希图中删除特定条目的简短方法

Short method to remove specific entries from hashmap

我一直在寻找一种简短易读的方法来从哈希图中删除条目。 具体来说,这是我的方法:

Map<String, HashMap<String, Long>> kitcooldowns = new HashMap<String, HashMap<String, Long>>();


// METHOD TO REMOVE EXPIRED COOLDOWNS FROM HASHMAP

final long currentime = System.currentTimeMillis();
final HashMap<String, HashMap<String, Long>> tmp = new HashMap<String, HashMap<String,Long>>();
for (final Entry<String, HashMap<String, Long>> kits : kitcooldowns.entrySet()) {
    final HashMap<String, Long> newitems = new HashMap<String, Long>();
    for (final Entry<String, Long> item : kits.getValue().entrySet()) {
        if (item.getValue() + getCooldownTime(item.getKey()) > currentime) {
            newitems.put(item.getKey(), item.getValue());
        }
    }
    if (newitems.isEmpty() == false) tmp.put(kits.getKey(), newitems);
}

kitcooldowns = tmp;


private long getCooldownTime(final String type) {
    switch (type) {
    case "CooldownX":
        return 3600000;
    case "CooldownY":
        return 1800000;
    default:
        return 0L;
    }
}

为了简化这个,这是主要结构:

MAP<NAME OF PLAYER, MAP<TYPE OF COOLDOWN, TIME WHEN USED>>

如果特定的冷却时间已过,玩家将从哈希图中移除。 现在,这对我来说似乎是一个混乱的解决方案,我相信有更好的解决方案。

编辑: 我的问题是,如果有一种高效且干净的方法(如迭代器)Java 8,它为大多数长方法提供了大量新的单行解决方案。

无需创建单独的地图。如果您遍历地图的 entrySet()values().

,则可以使用 Iterator#remove()
for (Iterator<Entry<String, Long>> iter = kitcooldowns.entrySet().iterator(); iter.hasNext();) {
  Entry<String, Long> entry = iter.next();
  if (entry.getValue() + getCooldownTime(entry.getKey()) > currentime) {
    iter.remove();
  }
}

OP想知道:

Isn't there any one-line solution with Java 8?

当然可以,但是 我强烈提醒您不要仅仅因为可以就把所有东西都写成一行。请记住,代码的存在是为了供未来的开发人员 阅读,而不是尽可能简洁地编写。此外,使用 Iterator#remove() 的代码将使用更少的内存,因为它不必复制地图。使用更少内存的代码最终也会变得更快,因为更少的内存使用会导致更少的 GC(花费 CPU 时间)和更少的 CPU 缓存未命中。

也就是说:

kitcooldowns = kitcooldowns.entrySet().stream()
  .filter(entry -> entry.getValue() + getCooldownTime(entry.getKey()) <= currentime)
  .collect(Collectors.toMap(Entry::getKey, Entry::getValue));

您可以简单地使用这个 Java 一行:

    final long currentime = System.currentTimeMillis();
    kitcooldowns.entrySet().removeIf(entry -> entry.getValue().entrySet()
    .removeIf(entry2 -> entry2.getValue() + getCooldownTime(entry2.getKey()) 
    < currentime) && entry.getValue().isEmpty());