二维数组简单实现HashMap

来源:互联网 发布:跳跃表 java 编辑:程序博客网 时间:2024/05/29 13:19
package com.ljb.test;


import java.lang.reflect.Array;


public class MyHashMap<E> {


private int length = 0;

private E[][] map = null;

private E[] keys = null;

/**
* 添加数据
* @param key
* @param value
*/
public void put(E key,E value){
if(this.length <1){
this.newArray((Class<? extends E[][]>) value.getClass(), 16);
this.newKey((Class<? extends E[]>) value.getClass(), 0);
}
if(this.length == 16){
this.addCapacity(6, (Class<? extends E[][]>) value.getClass());

}

this.addKeyCapacity(1, (Class<? extends E[]>) value.getClass());

map[this.length][0] = key;
map[this.length][1] = value;

keys[this.length] = key;

this.length++;
}

/**
* 取得数据
* @param key
* @return
*/
public E get(E key){

int rows = map.length;
for(int i = 0 ; i < rows ; i++ ){
if(key.equals(map[i][0])){
return map[i][1];
}
}

return null;
}
/**
* 初始化数组
* @param newType
* @param newLength
*/
private void  newArray(Class<? extends E[][]> newType,int newLength){
this. map = ((Object)newType == (Object)Object[][].class)
        ? (E[][]) new Object[newLength][2]
        : (E[][]) Array.newInstance(newType, new int[]{newLength,2});
}

private void newKey(Class<? extends E[]> newType,int newLength){
this.keys = ((Object)newType == (Object)Object[].class)
       ? (E[]) new Object[16]
               : (E[]) Array.newInstance(newType, newLength);
}

/**
* 增加数组长度
* @param addSize
* @param newType
*/
private void addCapacity(int addSize,Class<? extends E[][]> newType){
if(addSize < 1)
addSize = 10;
int newLength = this.length + addSize;
E[][] tmpMap =  ((Object)newType == (Object)Object[][].class)
       ? (E[][]) new Object[newLength][2]
               : (E[][]) Array.newInstance(newType, new int[]{newLength,2});
       
       System.arraycopy(this.map, 0, tmpMap, 0, this.length);
       
this.map = tmpMap;

}

private void addKeyCapacity(int addSize,Class<? extends E[]> newType){
if(addSize < 1)
addSize = 10;
int newLength = this.length + addSize;
E[] tmpMap =  ((Object)newType == (Object)Object[].class)
       ? (E[]) new Object[newLength]
               : (E[]) Array.newInstance(newType, newLength);
       
       System.arraycopy(this.keys, 0, tmpMap, 0, this.length);
       
this.keys = tmpMap;

}
/**
* 删除
* @param k
* @return
*/
public E remove(E k){
E val = null;
for(int i = 0 ; i < this.length ; i++){
if(this.map[i][0].equals(k)){
val = this.map[i][1];
this.subCapacity(i);
this.subKeyCapacity(i);
this.length--;
break;
}
}

return val;
}
/**
* keySet
* @return
*/
public E[] keyArray(){
return this.keys;
}
private void subCapacity(int index){
for(int i = index; i < this.length-1 ; i++){
this.map[i][0] = this.map[i+1][0];
this.map[i][1] = this.map[i+1][1];
}
}

private void subKeyCapacity(int index){
for(int i = index; i < this.length-1 ; i++){
this.keys[i] = this.keys[i+1];
this.keys[i] = this.keys[i+1];
}
}

public static void main(String[] args) {
MyHashMap<String> myMap = new MyHashMap<String>();
for(int i = 0 ; i < 20; i++){
myMap.put("01"+i, "Hello"+i);
}


String val = myMap.get("0119");


System.out.println(val); 


String removeVal = myMap.remove("0112");

System.out.println(removeVal);

System.out.println("-------------------");
String[] keys = myMap.keyArray();
for(int i = 0 ; i < keys.length; i++){
System.out.println(myMap.get(keys[i]));
}

MyHashMap<Integer> myMap2 = new MyHashMap<Integer>();
myMap2.put(1, 2);

Integer val2 = myMap2.get(1);

System.out.println(val2);
}

}
0 0
原创粉丝点击