java中使用数组和链表简单实现SJBMap

import java.util.LinkedList;

 

public class SJBMap {

 

    private Object[] elementData;

private int size;

 

 

public SJBMap(){

this(999);

}

 

private SJBMap(int initalCapital){

 

Object[]  array = new Object[initalCapital];

elementData = array;

 

}

 

public void add(Object key,Object value){

 

 

int hashCode = key.hashCode();

 

int min = hashCode % elementData.length;

 

Entry en = new Entry(key,value);

 

if(elementData[min] == null){

 

LinkedList list = new LinkedList();

 

list.add(en);

 

elementData[min] = list;

 

 

}else{

 

LinkedList list = (LinkedList) elementData[min];

 

list.add(en);

 

}

 

size++;

 

 

 

}

 

 

private Entry getkeys(Object key){

 

int min = key.hashCode() % elementData.length;

Entry en = null;

if(elementData[min] != null){

 

LinkedList list = (LinkedList) elementData[min];

 

for(int i=0;i<list.size();i++){

 

Entry entry = (Entry) list.get(i);

 

if(entry.key.equals(key)){

en = entry;

 

break;

}

}

}

 

return en;

 

}

 

 

public Object getKeys(Object key){

return getkeys(key).value;

}

 

 

 

class Entry{

 

private Object key;

 

private Object value;

 

public Entry(){

 

}

        public Entry(Object key,Object value){

this.key = key;

this.value = value;

}

 

public Object getKey() {

return key;

}

 

public void setKey(Object key) {

this.key = key;

}

 

public Object getValue() {

return value;

}

 

public void setValue(Object value) {

this.value = value;

}

 

}

 

}

posted @ 2016-09-14 10:16  sujianbo  阅读(353)  评论(0编辑  收藏  举报