I was looking for a short and easy readable method to remove entries from a hashmap. Specifically, this is my method:
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;
}
}
To simplify this, this is the main structure:
MAP<NAME OF PLAYER, MAP<TYPE OF COOLDOWN, TIME WHEN USED>>
If the specific cooldown has expired, the player gets removed from the Hashmap. Now, this looks like a messy solution to me and I am sure there is a better one.
EDIT: My Question is, if there is a efficient and clean method (like iterator) with Java 8, which has tons of new one-line solutions for most of the long methods.