Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
Map<Integer, Node> map = new HashMap<Integer, Node>(); // The head (eldest) of the doubly linked list. Node head; // The tail (youngest) of the doubly linked list. Node tail; int cap; publicLRUCache(int capacity){ cap = capacity; head = new Node(null, null); tail = new Node(null, null); head.next = tail; tail.pre = head; }