标签:efault main builder tor private href col app build
https://blog.csdn.net/Apeopl/article/details/90137398
import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; public class SimpleLRUCache<K, V> { private final int MAX_CACHE_SIZE; private final float DEFAULT_LOAD_FACTORY = 0.75f; LinkedHashMap<K, V> map; public SimpleLRUCache(int cacheSize) { MAX_CACHE_SIZE = cacheSize; int capacity = (int)Math.ceil(MAX_CACHE_SIZE / DEFAULT_LOAD_FACTORY) + 1; map = new LinkedHashMap<K, V>(capacity, DEFAULT_LOAD_FACTORY, true) { private static final long serialVersionUID = 1L; @Override protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { return size() > MAX_CACHE_SIZE; } }; } public synchronized void put(K key, V value) { map.put(key, value); } public synchronized V get(K key) { return map.get(key); } public synchronized void remove(K key) { map.remove(key); } public synchronized Set<Map.Entry<K, V>> getAll() { return map.entrySet(); } @Override public String toString() { StringBuilder stringBuilder = new StringBuilder(); for (Map.Entry<K, V> entry : map.entrySet()) { stringBuilder.append(String.format("%s: %s ", entry.getKey(), entry.getValue())); } return stringBuilder.toString(); } public static void main(String[] args) { SimpleLRUCache<String, String> lru = new SimpleLRUCache<>(5); lru.put("球员1", "杜兰特"); lru.put("球员2", "表妹"); lru.put("球员3", "库里"); System.out.println(lru); lru.get("球员1"); System.out.println(lru); lru.put("球员4", "一哥"); lru.put("球员5", "汤姆"); System.out.println(lru); lru.put("球员6", "格林"); System.out.println(lru); } }
标签:efault main builder tor private href col app build
原文地址:https://www.cnblogs.com/thiaoqueen/p/12796769.html