From 6ae50ed607136be17942d6107ffa57eb81d1111d Mon Sep 17 00:00:00 2001 From: JianXinyi <1259606552@qq.com> Date: Thu, 23 Apr 2026 18:24:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'w'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w/Cache.java | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 w/Cache.java 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