You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

50 lines
1.2 KiB

import java.util.HashMap;
import java.util.Map;
public class Cache<K, V> {
private final Map<K, V> cache;
public Cache() {
this.cache = new HashMap<>();
}
public void put(K key, V value) {
cache.put(key, value);
}
public V get(K key) {
return cache.get(key);
}
public boolean containsKey(K key) {
return cache.containsKey(key);
}
public void remove(K key) {
cache.remove(key);
}
public void clear() {
cache.clear();
}
public int size() {
return cache.size();
}
public static void main(String[] args) {
Cache<String, Integer> studentCache = new Cache<>();
studentCache.put("张三", 18);
studentCache.put("李四", 20);
System.out.println("张三的年龄:" + studentCache.get("张三"));
System.out.println("缓存是否包含王五:" + studentCache.containsKey("王五"));
studentCache.remove("李四");
System.out.println("移除李四后缓存大小:" + studentCache.size());
studentCache.clear();
System.out.println("清空后缓存大小:" + studentCache.size());
}
}