From b68a7c797ee0ea5e5854eb7635c9ad929db8a0dc Mon Sep 17 00:00:00 2001 From: Mengxinyao Date: Mon, 1 Jun 2026 00:16:03 +0800 Subject: [PATCH] =?UTF-8?q?feat(w8):W8-=E5=AD=9F=E9=91=AB=E5=9E=9A-2025060?= =?UTF-8?q?10204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w8/.DS_Store | Bin 0 -> 6148 bytes w8/work8/.gitignore | 30 +++++++++++++++++++ w8/work8/.idea/.gitignore | 10 +++++++ w8/work8/.idea/misc.xml | 6 ++++ w8/work8/.idea/modules.xml | 8 +++++ w8/work8/src/Cache.java | 58 +++++++++++++++++++++++++++++++++++++ w8/work8/src/Pair.java | 41 ++++++++++++++++++++++++++ w8/work8/src/Test.java | 21 ++++++++++++++ w8/work8/work8.iml | 11 +++++++ w8/work8/泛型作业 | 38 ++++++++++++++++++++++++ 10 files changed, 223 insertions(+) create mode 100644 w8/.DS_Store create mode 100644 w8/work8/.gitignore create mode 100644 w8/work8/.idea/.gitignore create mode 100644 w8/work8/.idea/misc.xml create mode 100644 w8/work8/.idea/modules.xml create mode 100644 w8/work8/src/Cache.java create mode 100644 w8/work8/src/Pair.java create mode 100644 w8/work8/src/Test.java create mode 100644 w8/work8/work8.iml create mode 100644 w8/work8/泛型作业 diff --git a/w8/.DS_Store b/w8/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..0311984080c4bf0974a1356262382aab7f46ea32 GIT binary patch literal 6148 zcmeHK%}T>S5Z>*NO(;SR3OxqA7ENstiUY%mNUNFAGt8*ceu&{&hA|7OYYwK8~(;!X~0|>dkjMGSDo|vW4 zMCSTx!Y~c9Z#Cxgw&OJIPOH6W+Vk!{+6SG*!Zfyb_Kwc_!{jj)&+?rahi_BLrp5wZ zz*t(*lRr)qk=}#1$S5KSi2-7O7+5a`^eJc5*K1j{8e)JL_yq>=e6T?gU5&Xyd33-a zTL8ccxTS!NKaQAVYjic{3SkC>t5iUh%5{suRXX@>9p`Gy6{>W`b@RdXlez9txO_U? z-K{OBp#K3woz$-n!=fT=^ZCzO<)><3%78C{J na)qBMV8~Jou~>>5pi;nZqXFn@%oTzMgnk4h4Kxq~f6BlIUOQFM literal 0 HcmV?d00001 diff --git a/w8/work8/.gitignore b/w8/work8/.gitignore new file mode 100644 index 0000000..13275f1 --- /dev/null +++ b/w8/work8/.gitignore @@ -0,0 +1,30 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ +.kotlin + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/w8/work8/.idea/.gitignore b/w8/work8/.idea/.gitignore new file mode 100644 index 0000000..b6b1ecf --- /dev/null +++ b/w8/work8/.idea/.gitignore @@ -0,0 +1,10 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 已忽略包含查询文件的默认文件夹 +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ diff --git a/w8/work8/.idea/misc.xml b/w8/work8/.idea/misc.xml new file mode 100644 index 0000000..6f29fee --- /dev/null +++ b/w8/work8/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/w8/work8/.idea/modules.xml b/w8/work8/.idea/modules.xml new file mode 100644 index 0000000..6e97e66 --- /dev/null +++ b/w8/work8/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/w8/work8/src/Cache.java b/w8/work8/src/Cache.java new file mode 100644 index 0000000..41e0707 --- /dev/null +++ b/w8/work8/src/Cache.java @@ -0,0 +1,58 @@ +import java.util.*; + +/** + * 泛型缓存类,支持基本的增删查操作 + * @param 键类型 + * @param 值类型 + */ +public class Cache { + private Map map; + private int maxSize; + + public Cache(int maxSize) { + this.maxSize = maxSize; + this.map = new LinkedHashMap(maxSize, 0.75f, true) { + @Override + protected boolean removeEldestEntry(Map.Entry eldest) { + return size() > maxSize; + } + }; + } + + public void put(K key, V value) { + map.put(key, value); + } + + public V get(K key) { + return map.get(key); + } + + public boolean containsKey(K key) { + return map.containsKey(key); + } + + public V remove(K key) { + return map.remove(key); + } + + public int size() { + return map.size(); + } + + public void clear() { + map.clear(); + } + + public Set keySet() { + return map.keySet(); + } + + public Collection values() { + return map.values(); + } + + @Override + public String toString() { + return "Cache{" + map + "}"; + } +} \ No newline at end of file diff --git a/w8/work8/src/Pair.java b/w8/work8/src/Pair.java new file mode 100644 index 0000000..316064d --- /dev/null +++ b/w8/work8/src/Pair.java @@ -0,0 +1,41 @@ +/** + * 泛型 Pair 类,用于存储两个相关联的对象 + * @param 键类型 + * @param 值类型 + */ +public class Pair { + private K key; + private V value; + + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + + public K getKey() { + return key; + } + + public V getValue() { + return value; + } + + /** + * 交换 key 和 value 的值 + * 注意:仅当 K 和 V 类型可相互赋值时有效 + */ + public void swap() { + // 使用 Object 临时存储,强制转换(假设类型兼容) + Object temp = key; + key = (K) value; + value = (V) temp; + } + + @Override + public String toString() { + return "Pair{" + + "key=" + key + + ", value=" + value + + '}'; + } +} \ No newline at end of file diff --git a/w8/work8/src/Test.java b/w8/work8/src/Test.java new file mode 100644 index 0000000..6f94589 --- /dev/null +++ b/w8/work8/src/Test.java @@ -0,0 +1,21 @@ +public class Test { + public static void main(String[] args) { + // 测试 Pair + Pair pair = new Pair<>("hello", 123); + System.out.println("原始: " + pair); + + pair.swap(); + System.out.println("交换后: " + pair); // 输出: Pair{key=123, value=hello} + + // 测试 Cache + Cache cache = new Cache<>(3); + cache.put("a", 1); + cache.put("b", 2); + cache.put("c", 3); + System.out.println("缓存内容: " + cache); + + System.out.println("获取 a: " + cache.get("a")); + cache.put("d", 4); // 超出容量,移除最久未使用的 "a" + System.out.println("添加 d 后: " + cache); + } +} \ No newline at end of file diff --git a/w8/work8/work8.iml b/w8/work8/work8.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/w8/work8/work8.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/w8/work8/泛型作业 b/w8/work8/泛型作业 new file mode 100644 index 0000000..0786f2c --- /dev/null +++ b/w8/work8/泛型作业 @@ -0,0 +1,38 @@ +/** + * 泛型 Pair 类,用于存储两个相关联的对象 + * @param 键类型 + * @param 值类型 + */ +public class Pair { + private K key; + private V value; + + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + + public K getKey() { + return key; + } + + public V getValue() { + return value; + } + + /** + * 交换 key 和 value 的值(注意:仅适用于可互换类型的场景) + * 注意:如果 K 和 V 类型不同,交换后可能语义不一致 + */ + public void swap() { + // 使用 Object 临时存储,再交换 + Object temp = key; + key = value; + value = (V) temp; // 强转,需确保类型兼容 + } + + @Override + public String toString() { + return "(" + key + ", " + value + ")"; + } +}