diff --git a/w/Cache.java b/w/Cache.java new file mode 100644 index 0000000..18fa2f9 --- /dev/null +++ b/w/Cache.java @@ -0,0 +1,51 @@ +package w8; + +import java.util.HashMap; +import java.util.Map; + +public class Cache { + private final Map cache = new HashMap<>(); + + // 存入缓存 + public void put(K key, V value) { + cache.put(key, value); + } + + // 从缓存获取 + public V get(K key) { + return cache.get(key); + } + + // 移除缓存 + public V remove(K key) { + return cache.remove(key); + } + + // 清空缓存 + public void clear() { + cache.clear(); + } + + // 判断是否包含key + public boolean containsKey(K key) { + return cache.containsKey(key); + } + + // 获取缓存大小 + public int size() { + return cache.size(); + } + + // 测试 + public static void main(String[] args) { + Cache studentScore = new Cache<>(); + studentScore.put("张三", 90); + studentScore.put("李四", 85); + + System.out.println("张三的分数:" + studentScore.get("张三")); + System.out.println("缓存大小:" + studentScore.size()); + + studentScore.remove("李四"); + System.out.println("移除李四后,缓存是否包含李四?" + studentScore.containsKey("李四")); + } +} \ No newline at end of file