用java中的HashMap实现队列
2008-12-04 09:18
351 查看
import java.util.*;
public class VarCostRateQueue {
private final static int size=20;
private static HashMap storage=new HashMap(size);
private static String[] keySet=new String[size];
private static int head=-1,trail=-1;
public VarCostRateQueue(){}
public static void push(String key,Object obj)throws Exception
{
synchronized(storage)
{
synchronized(keySet)
{
if(storage.get(key)==null)
{
trail=(trail+1)%20;
if(trail==head)
{
storage.remove(keySet[head]);
head=(head+1)%20;
storage.put(key, obj);
keySet[trail]=key;
}else
{
storage.put(key, obj);
keySet[trail]=key;
if(head==-1)
head=0;
}
}
}
}
}
public static Object get(String key)throws Exception
{
return storage.get(key);
}
private static void clear()throws Exception
{
synchronized(storage)
{
storage.clear();
head=-1;
trail=-1;
}
}
}
相关文章推荐
- HashMap的原理与实 无锁队列的实现Java HashMap的死循环 red black tree
- Java-用数组实现队列(简)
- Java5实现阻塞队列
- Java:实现简单的阻塞队列
- 深入Java集合学习系列:HashMap的实现原理
- Java数组实现队列
- java队列实现(顺序队列、链式队列、循环队列)
- Java中阻塞队列的几种实现方式
- Java集合学习:HashMap的实现原理和工作原理
- 《剑指offer2》问题9 用两个栈实现队列 Java实现
- 数据结构之队列【java实现】
- [Java] HashMap的实现
- Java数组实现循环队列、Java链表实现队列
- 深入Java集合学习系列:HashMap的实现原理
- Java HashMap实现详解
- JAVA容器-浅谈HashMap的实现原理
- java 中HashMap实现原理深入理解
- Java 源码分析HashMap的工作原理及实现
- 剑指offer编程题Java实现——面试题7用两个栈实现队列
- 数据结构之优先队列--二叉堆(Java实现)