前几天去一个公司面试,面试官直接让上机写一个LRU缓存,当时写的很乱,现整理如下:package com.jd.test; import java.io.Serializable; import java.util.LinkedHashMap; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * 缓存类(最近最少未使用) * * @author liuzhenfeng * * @param <K,V> */ public class LRUCache<K, V> extends LinkedHashMap<K, V> implements Serializable { /** * 缓存默认大小 */ public static final int DEFAULT_CAPASITY = 20; /** * 缓存实际大小 */ public static int CACHE_CAPASITY = DEFAULT_CAPASITY; /** * 线程同步锁 */ private static final Lock lock = new ReentrantLock(); public LRUCache() { super(DEFAULT_CAPASITY); CACHE_CAPASITY = DEFAULT_CAPASITY; } public LRUCache(int size) { super(size); CACHE_CAPASITY = size; } /* * 清空緩存 * * @see java.util.LinkedHashMap#clear() */ @Override public