package algorithm;
/**
* @author Administrator
*定义hashMap中的每个单元的数据结构
*/
public class Hash {
private int hashCode; //用来存储hash值
private int py; //是否放置元素标志位
private int times; //标记元素出现的次数
private LinkList head;//该单元的头节点
private LinkList tail;//该单元的尾节点
public Hash() //初始化hash单元
{
py=0;
times=0;
hashCode=-1;
LinkList head=null;
LinkList tail=null;
}
public Object getKey()
{
return hashCode;
}
public void setKey(int key)
{
this.hashCode = key;
}
public int getPy()
{
return py;
}
public void setPy(int py)
{
this.py = py;
}
public LinkList getHead()
{
return head;
}
public void setHead(LinkList head)
{
this.head = head;
}
public LinkList getTail()
{
return tail;
}
public void setTail(LinkList tail)
{
this.tail = tail;
}
public void insert(Object o1,Object o2)//堆hash值相同的对象,把对象插入链表中
{
LinkList addNode=new LinkList(o1,o2);//新建节点
addNode.nextNode=null;
if(head==null)//第一次插入
{
tail=addNode;
head=addNode;
head.nextNode=addNode;
}
else//在未节点插入
{
tail.nextNode=addNode;
tail=addNode;
}
}
public boolean find(Object o)//查找对象,调用toString函数,如过两个的toString的值相同,则,找到
{
LinkList p=head;
while(p!=null)//遍历链表
{
if(p.getKey().toString().equals(o.toString()))//判断toString函数是否相等
{
return true;
}
else
p=p.nextNode;
}
if(p==null)
System.out.println("没找到!");
return false;
}
public class LinkList
{
private Object value;
private Object key;
LinkList nextNode;
public LinkList(Object o1,Object o2)
{
this.value=o2;
this.key=o1;
LinkList nextNode=null;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public Object getKey() {
return key;
}
public void setKey(Object key) {
this.key = key;
}
public LinkList getNextNode() {
return nextNode;
}
public void setNextNode(LinkList nextNode) {
this.nextNode = nextNode;
}
}
public int getTimes()
{
return times;
}
public void setTimes()
{
times++;
}
}
</pre><pre name="code" class="java">以上是hashMap的每个节点数据结构<pre name="code" class="java">package algorithm;
/**
* @author Administrator
*本程序主要演示了hashMap的底层实现原理:hashMap其实就是一个数组,数组的每个单元装载hash值
*这个数组的每个单元又含有一个链表,链表中装载hash值相同但是key又不相同的对象
*计算hash值的方法很多,本程序的采用的是:调用对象的toString(),函数,然后让得到的字符串
*的每个字符累计相加,得到的结果为sum,然后让sum对小于hash容量的最大的质数取余数。
*本程序,容量为1000,质数为997;
*
*
*
*/
public class HashMap {//定义hashMap
Hash hash[]=new Hash[1000];//定义hashMap的容量
public HashMap()//对hashMap每个hash单元进行初始化
{
for(int i=0;i<1000;i++)
hash[i]=new Hash();
}
private int getHashcode( String s) //获取hash值
{
char[] array=s.toCharArray();
int sum=0;
for(int i=0;i<array.length;i++)
sum+=(int )array[i];
return sum%999;
}
public void add(Object o1,Object o2) //添加元素
{
Hash node;
int i=getHashcode(o1.toString());
node=hash[i];
if(node.getPy()==0)
{
node.setKey(i);
node.insert(o1,o2);
node.setPy(1);
node.setTimes();
}
else
{
node.insert(o1,o2);
node.setTimes();
}
}
public void find(Object key) //查找元素
{
int i=getHashcode(key.toString());
Hash node=(Hash)hash[i];
if(node.getPy()==0)
System.out.println("表中没有该元素!");
else
{
boolean isExist=node.find(key);
if(isExist==true)
System.out.println("已经找到!!位置是哈希表索引为"+i+"的位置");
else
System.out.println("表中没有该元素!");
}
}
public static void main(String[] args) {
HashMap hashMap=new HashMap();
hashMap.add("heheh","chenliang");
hashMap.add("hahha", "chensiyang");
hashMap.find("hahha");
}
}
以上是hashMap的底层数据结构和实现原理