# Solution using HashMap and DoubleLinkedList

• ``````public class LRUCache {
public static class Node {
int key;
int value;
Node next;
Node prev;

Node(int key, int value) {
this.key = key;
this.value = value;
this.prev = null;
this.next = null;
}
}

Node tail;
int size;

this.size = 0;
this.tail = null;
}

public Node add(int key, int value) {
Node n = new Node(key, value);

if (tail == null) {
}
else {
tail.next = n;
n.prev = tail;
}

tail = n;
size++;
return n;
}

public Node removeLRU() {

tail = null;
size--;
}

else if (head != null) {
size--;
}

}

public void moveToTail(Node n) {
if(tail == n){
return;
}

n.next.prev = null;
}
else {
n.prev.next = n.next;
n.next.prev = n.prev;
}

tail.next = n;
n.prev = tail;
n.next = null;
tail = n;
}

//for debugging purpose
public void print() {
System.out.print("(");
while(cur!= null) {
System.out.print(cur.key+" ");
cur = cur.next;
}
System.out.println(")");
}

}

int capacity;
Map<Integer, Node> map;

public LRUCache(int capacity) {
this.map = new HashMap<Integer, Node>();
this.capacity = capacity;
}

public int get(int key) {
if(map.containsKey(key)) {
Node n = map.get(key);
return (int) map.get(key).value;
}
else {
return -1;
}
}

public void set(int key, int value) {
if (map.containsKey(key)) {
Node n = map.get(key);
n.value = value;
map.put(key, n);
}
else if (linkedList.size == this.capacity) {