public class ChainingHashSet {
public static class HashSet<K,V>{
private int num;
private int capacity;
private SearchNode<K,V>[] st;
public HashSet(int init) {
capacity=init;
st=(SearchNode<K,V>[]) new Object[capacity];
for(int i=0;i<capacity;i++) {
st[i]=new SearchNode<>();
}
}
private int hash(K key) {
return (key.hashCode() & 0x7fffffff) % capacity;
}
private V get( K key) {
return st[hash(key)].get(key);
}
public void put(K key, V value) {
st[hash(key)].put(key, value);
}
}
public static class SearchNode<K,V>{
private Node first;
private class Node{
K key;
V value;
Node next;
public Node(K key,V val,Node next) {
this.key=key;
this.value=value;
this.next=next;
}
}
public V get(K key) {
for(Node node=first;node!=null;node=node.next) {
if(key.equals(node.key))
return node.value;
}
return null;
}
public void put(K key, V value) {
Node node;
for(node=first;node!=null;node=node.next) {
if(key.equals(key)) {
node.value=value;
}
}
first=new Node(key,value,first);
}
}
}
mport java.util.HashMap;
public class LRUCache<K,V> {
private int CacheCapacity;
private HashMap<K,CacheNode> caches;
private CacheNode first;
private CacheNode last;
class CacheNode{
CacheNode pre;
CacheNode next;
Object key;
Object value;
public CacheNode() {
}
}
public LRUCache(int size) {
this.CacheCapacity=size;
caches=new HashMap<K,CacheNode>(size);
}
public void put(K k,V v) {
CacheNode node=caches.get(k);
if(node==null) {
if(caches.size()>=CacheCapacity) {
caches.remove(last.key);
removeLast();
}
node=new CacheNode();
node.key=k;
}
node.value=v;
moveToFirst(node);
caches.put(k, node);
}
public Object get(K k) {
CacheNode node=caches.get(k);
if(node==null) {
return null;
}
moveToFirst(node);
return node.value;
}
public Object remove(K k) {
CacheNode node=caches.get(k);
if(node!=null) {
if(node.pre!=null) {
node.pre.next=node.next;
}
if(node.next!=null) {
node.next.pre=node.pre;
}
if(node==first) {
first=node.next;
}
if(node==last) {
last=node.pre;
}
}
return caches.remove(k);
}
public void clear() {
first=null;
last=null;
caches.clear();
}
private void moveToFirst(CacheNode node) {
if(first==node) {
return;
}
if(node.next!=null) {
node.next.pre=node.pre;
}
if(node.pre!=null) {
node.pre.next=node.next;
}
if(node==last) {
last=last.pre;
}
if(first==null||last==null) {
first=last=node;
return;
}
node.next=first;
first.pre=node;
first=node;
first.pre=null;
}
private void removeLast() {
if(last!=null) {
last=last.pre;
if(last==null) {
first=null;
}else {
last.next=null;
}
}
}
}