import java.util.HashMap; import java.util.Map; public class Cache{ private Map cache; public Cache(){ this.cache=new HashMap<>(); } //存储键值对到缓存 public void put(K key,V value){ cache.put(key,value); } //根据键获取值 public V get(K key){ return cache.get(key); } //检查某个缓存是否包含某个键 public boolean containsKey(K key){ return cache.containsKey(key); } //移除指定键的值 public V remove(K key){ return cache.remove(key); } //清空缓存 public void clear(){ cache.clear(); } //获取缓存大小 public int size(){ return cache.size(); } //检查缓存是否为空 public boolean isEmpty(){ return cache.isEmpty(); } @Override public String toString(){ return "Cache"+cache.toString(); } public static void main (String []args){ Cache ageCache=new Cache<>(); ageCache.put("张三",14); ageCache.put("李四",13); ageCache.put("王五",16); System.out.println("年龄缓存:"+ageCache); System.out.println("获取张三年龄"+ageCache.get("张三")); System.out.println("缓存大小:"+ageCache.size()); System.out.println("是否存在赵强的信息:"+ageCache.containsKey("赵强")); } }