3 changed files with 145 additions and 0 deletions
@ -0,0 +1,75 @@ |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
import java.util.concurrent.locks.ReentrantReadWriteLock; |
|||
|
|||
public class Cache<K, V> { |
|||
private final Map<K, V> 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<String, Integer> 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()); |
|||
} |
|||
} |
|||
@ -0,0 +1,37 @@ |
|||
import java.lang.reflect.*; |
|||
import java.util.*; |
|||
|
|||
public class GenericReflectionDemo { |
|||
private Map<String, List<Integer>> complexField; |
|||
|
|||
public List<Double> process(Map<String, ? extends Number> 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())); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
public class Pair<K, V> { |
|||
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<V, K> swap() { |
|||
return new Pair<>(this.value, this.key); |
|||
} |
|||
|
|||
@Override |
|||
public String toString() { |
|||
return "Pair{" + key + " -> " + value + "}"; |
|||
} |
|||
|
|||
public static void main(String[] args) { |
|||
Pair<String, Integer> p1 = new Pair<>("age", 25); |
|||
System.out.println("原始: " + p1); |
|||
Pair<Integer, String> p2 = p1.swap(); |
|||
System.out.println("交换后: " + p2); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue