From 422820bcce234f98650d769cff02ea305e954d72 Mon Sep 17 00:00:00 2001 From: LiuZihan <1353843969@qq.com> Date: Wed, 29 Apr 2026 21:51:20 +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'w8'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w8/Cache.java | 75 +++++++++++++++++++++++++++++++++++ w8/GenericReflectionDemo.java | 37 +++++++++++++++++ w8/Pair.java | 33 +++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 w8/Cache.java create mode 100644 w8/GenericReflectionDemo.java create mode 100644 w8/Pair.java diff --git a/w8/Cache.java b/w8/Cache.java new file mode 100644 index 0000000..ffc1f18 --- /dev/null +++ b/w8/Cache.java @@ -0,0 +1,75 @@ +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +public class Cache { + private final Map store = new HashMap<>(); + private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + + public void put(K key, V value) { + if (key == null || value == null) + throw new IllegalArgumentException("Key and value must not be null"); + lock.writeLock().lock(); + try { + store.put(key, value); + } finally { + lock.writeLock().unlock(); + } + } + + public V get(K key) { + lock.readLock().lock(); + try { + return store.get(key); + } finally { + lock.readLock().unlock(); + } + } + + public boolean containsKey(K key) { + lock.readLock().lock(); + try { + return store.containsKey(key); + } finally { + lock.readLock().unlock(); + } + } + + public V remove(K key) { + lock.writeLock().lock(); + try { + return store.remove(key); + } finally { + lock.writeLock().unlock(); + } + } + + public void clear() { + lock.writeLock().lock(); + try { + store.clear(); + } finally { + lock.writeLock().unlock(); + } + } + + public int size() { + lock.readLock().lock(); + try { + return store.size(); + } finally { + lock.readLock().unlock(); + } + } + + public static void main(String[] args) { + Cache cache = new Cache<>(); + cache.put("one", 1); + cache.put("two", 2); + System.out.println(cache.get("one")); + System.out.println(cache.get("two")); + cache.remove("one"); + System.out.println(cache.containsKey("one")); + System.out.println("Cache size: " + cache.size()); + } +} \ No newline at end of file diff --git a/w8/GenericReflectionDemo.java b/w8/GenericReflectionDemo.java new file mode 100644 index 0000000..a559da2 --- /dev/null +++ b/w8/GenericReflectionDemo.java @@ -0,0 +1,37 @@ +import java.lang.reflect.*; +import java.util.*; + +public class GenericReflectionDemo { + private Map> complexField; + + public List process(Map map, int value) { + return new ArrayList<>(); + } + + public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException { + Field field = GenericReflectionDemo.class.getDeclaredField("complexField"); + Type genericType = field.getGenericType(); + System.out.println("===== 字段泛型信息 ====="); + if (genericType instanceof ParameterizedType) { + ParameterizedType pt = (ParameterizedType) genericType; + System.out.println("原始类型: " + pt.getRawType()); + System.out.println("键的类型: " + pt.getActualTypeArguments()[0]); + System.out.println("值的类型: " + pt.getActualTypeArguments()[1]); + } + + Method method = GenericReflectionDemo.class.getDeclaredMethod("process", Map.class, int.class); + Type returnType = method.getGenericReturnType(); + System.out.println("\n===== 方法返回类型泛型 ====="); + System.out.println("返回类型: " + returnType); + + Type[] paramTypes = method.getGenericParameterTypes(); + System.out.println("\n===== 方法参数泛型 ====="); + for (Type param : paramTypes) { + System.out.println("参数类型: " + param); + if (param instanceof ParameterizedType) { + ParameterizedType ppt = (ParameterizedType) param; + System.out.println(" -> 实际类型参数: " + Arrays.toString(ppt.getActualTypeArguments())); + } + } + } +} \ No newline at end of file diff --git a/w8/Pair.java b/w8/Pair.java new file mode 100644 index 0000000..49fd88a --- /dev/null +++ b/w8/Pair.java @@ -0,0 +1,33 @@ +public class Pair { + private final K key; + private final V value; + + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + + public K getKey() { + return key; + } + + public V getValue() { + return value; + } + + public Pair swap() { + return new Pair<>(this.value, this.key); + } + + @Override + public String toString() { + return "Pair{" + key + " -> " + value + "}"; + } + + public static void main(String[] args) { + Pair p1 = new Pair<>("age", 25); + System.out.println("原始: " + p1); + Pair p2 = p1.swap(); + System.out.println("交换后: " + p2); + } +} \ No newline at end of file