import java.util.HashMap; import java.util.Map; public class Cache { private final 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 void remove(K key) { cache.remove(key); } public void clear() { cache.clear(); } public int size() { return cache.size(); } public static void main(String[] args) { Cache studentCache = new Cache<>(); studentCache.put("张三", 18); studentCache.put("李四", 20); System.out.println("张三的年龄:" + studentCache.get("张三")); System.out.println("缓存是否包含王五:" + studentCache.containsKey("王五")); studentCache.remove("李四"); System.out.println("移除李四后缓存大小:" + studentCache.size()); studentCache.clear(); System.out.println("清空后缓存大小:" + studentCache.size()); } }