# 1797. Design Authentication Manager ###### tags: `Leetcode` `Medium` `HashMap` Link: https://leetcode.com/problems/design-authentication-manager/ ## 思路 注意在countUnexpiredTokens function里面由于想要清除掉expired tokens所以用了removeIf function 需要记住用法 因为如果换成 ```java for(int num:map.keySet()){ if(map.get(num)<=currentTime) map.remove(num) } ``` 会报错,因为不能在遍历的key set的时候删掉key ## Code ```java= class AuthenticationManager { int time; Map<String, Integer> map; public AuthenticationManager(int timeToLive) { time = timeToLive; map = new HashMap<>(); } public void generate(String tokenId, int currentTime) { map.put(tokenId, currentTime+time); } public void renew(String tokenId, int currentTime) { if(map.containsKey(tokenId) && map.get(tokenId)>currentTime) map.put(tokenId, currentTime+time); } public int countUnexpiredTokens(int currentTime) { map.entrySet().removeIf(e -> e.getValue() <= currentTime); return map.size(); } } ```