Java 从hashmap中删除特定项的简短方法

Java 从hashmap中删除特定项的简短方法,java,hashmap,Java,Hashmap,我一直在寻找一种简单易读的方法来从hashmap中删除条目。 具体来说,这是我的方法: Map<String, HashMap<String, Long>> kitcooldowns = new HashMap<String, HashMap<String, Long>>(); // METHOD TO REMOVE EXPIRED COOLDOWNS FROM HASHMAP final long currentime = System.c

我一直在寻找一种简单易读的方法来从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;
    }
}
mapkitcooldowns=newhashmap();
//方法从HASHMAP中删除过期的冷却时间
最终长CurrentTime=System.currentTimeMillis();
final HashMap tmp=new HashMap();
对于(最终入门工具包:kitcollowns.entrySet()){
final HashMap newitems=新HashMap();
对于(最终条目项:kits.getValue().entrySet()){
if(item.getValue()+getCooldownTime(item.getKey())>CurrentTime){
newitems.put(item.getKey(),item.getValue());
}
}
if(newitems.isEmpty()==false)tmp.put(kits.getKey(),newitems);
}
冷却时间=tmp;
私有长getCooldownTime(最终字符串类型){
开关(类型){
案例“冷却X”:
返回3600000;
案例“冷静”:
归还180万;
违约:
返回0升;
}
}
为了简化这一点,主要结构如下:

MAP<NAME OF PLAYER, MAP<TYPE OF COOLDOWN, TIME WHEN USED>>
MAP
如果特定的冷却时间已过期,玩家将从哈希图中移除。 现在,对我来说,这看起来是一个混乱的解决方案,我相信有一个更好的解决方案

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

无需创建单独的映射。如果迭代映射的
entrySet()
值()
,则可以使用


您可以简单地使用以下Java one liner:

    final long currentime = System.currentTimeMillis();
    kitcooldowns.entrySet().removeIf(entry -> entry.getValue().entrySet()
    .removeIf(entry2 -> entry2.getValue() + getCooldownTime(entry2.getKey()) 
    < currentime) && entry.getValue().isEmpty());
final long currentime=System.currentTimeMillis();
kitcollowns.entrySet().removeIf(entry->entry.getValue().entrySet())
.removeIf(entry2->entry2.getValue()+getCooldownTime(entry2.getKey())
Java 8是否有任何单行解决方案?提前谢谢。当然,你可以用流、过滤器等来实现。但我认为这不会让代码变得更干净。为什么它必须是一行呢?请看我的编辑,尽管出于上面列出的原因,我真的不能原谅它。
kitcooldowns = kitcooldowns.entrySet().stream()
  .filter(entry -> entry.getValue() + getCooldownTime(entry.getKey()) <= currentime)
  .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
    final long currentime = System.currentTimeMillis();
    kitcooldowns.entrySet().removeIf(entry -> entry.getValue().entrySet()
    .removeIf(entry2 -> entry2.getValue() + getCooldownTime(entry2.getKey()) 
    < currentime) && entry.getValue().isEmpty());