散列表

  • 实现一个基于链表法解决冲突问题的散列表
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);
    	}
    }
}
  • 实现一个 LRU 缓存淘汰算法
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;
    		}
    	}
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值