Problem: Define a fixed-size LRU cache that supports amortized O(1) operations on objects of type E. Each object has a unique id. This question contains a problem description and several solutions: LRU cache in Java with Generics and O(1) operations
Possible Solution that does not work: Create only one linked list and only one HashMap. Maintain LinkedList and HashMap<E.id, LinkedList>. The hashmap contains the address of each node in the linkedlist.
Deleting an entry from the cache can be done by:
node = hashmap.get(elem.id);
node.prev.next = node.next;
node.next.prev = node.prev;
hashmap.remove(elem.id)
(ignoring corner cases here for brevity)
The problem here is that Java's LinkedList class does not seem to expose individual nodes in the list. So I can't store their addresses in the hashmap. Is there a workaround for this? Most existing solutions implement their own DoublyLinkedList class. Can I use/extend an existing class in Java Collections Framework instead?