@@ -0,0 +1,2764 @@
+import lombok.extern.slf4j.Slf4j;%0A%0Aimport java.util.Collections;%0Aimport java.util.HashMap;%0Aimport java.util.LinkedHashMap;%0Aimport java.util.Map;%0A%0A@Slf4j%0Apublic class Solution %7B%0A%0A    public static void main(String%5B%5D args) %7B%0A        Solution solution = new Solution();%0A    %7D%0A%7D%0A%0Aclass Item%3CK, V%3E %7B%0A    public K key;%0A    public V value;%0A    public Item%3CK, V%3E prev;%0A    public Item%3CK, V%3E next;%0A%0A    public Item(K key, V value) %7B%0A        this.key = key;%0A        this.value = value;%0A    %7D%0A%7D%0A%0Aclass LRUCache%3CK, V%3E %7B%0A    int capacity;%0A    Map%3CK, Item%3CK, V%3E%3E map = new HashMap%3CK, Item%3CK, V%3E%3E();%0A    Item%3CK, V%3E head = null, tail = null;%0A%0A    public LRUCache(int capacity) %7B%0A        this.capacity = capacity;%0A    %7D%0A%0A    public V get(int key) %7B%0A        if (map.containsKey(key)) %7B%0A            Item%3CK, V%3E item = map.get(key);%0A            remove(item);%0A            setHead(item);%0A            return item.value;%0A        %7D%0A        return null;%0A    %7D%0A%0A    private void remove(Item%3CK, V%3E item) %7B%0A        if (item.prev != null) %7B%0A            item.prev.next = item.next;%0A        %7D else %7B%0A            head = item.next;%0A        %7D%0A        if (item.next != null) %7B%0A            item.next.prev = item.prev;%0A        %7D else %7B%0A            tail = item.prev;%0A        %7D%0A    %7D%0A%0A    private void setHead(Item%3CK, V%3E item) %7B%0A        item.next = head;%0A        item.prev = null;%0A%0A        if (head != null)%0A            head.prev = item;%0A        head = item;%0A%0A        if (tail == null)%0A            tail = head;%0A    %7D%0A%0A    public void set(K key, V value) %7B%0A        if (map.containsKey(key)) %7B%0A            Item item = map.get(key);%0A            item.value = value;%0A            remove(item);%0A            setHead(item);%0A        %7D else %7B%0A            Item item = new Item(key, value);%0A            if (map.size() %3E capacity) %7B%0A                map.remove(tail.key);%0A                remove(tail);%0A                setHead(item);%0A            %7D else %7B%0A                setHead(item);%0A            %7D%0A            map.put(key, item);%0A        %7D%0A    %7D%0A%7D%0A%0A%0Aclass LRUCacheWithLibrary%3CK, V%3E %7B%0A    private static final float hashTableLoadFactor = 0.75f;%0A%0A    private Map%3CK, V%3E map;%0A    private int cacheSize;%0A%0A    public LRUCacheWithLibrary(int cacheSize) %7B%0A        this.cacheSize = cacheSize;%0A        int hashTableCapacity = (int) Math.ceil(cacheSize / hashTableLoadFactor) + 1;%0A        map = Collections.synchronizedMap(new LinkedHashMap%3CK, V%3E(hashTableCapacity, hashTableLoadFactor, true) %7B%0A            @Override%0A            protected boolean removeEldestEntry(Map.Entry%3CK, V%3E eldest) %7B%0A                return size() %3E LRUCacheWithLibrary.this.cacheSize;%0A            %7D%0A        %7D);%0A    %7D%0A%0A    public V get(K key) %7B%0A        return map.get(key);%0A    %7D%0A%0A    public void put(K key, V value) %7B%0A        map.put(key, value);%0A    %7D%0A%0A%7D%0A

146. LRU Cache