/**
* Description:新建一个类作为map的key
*/
public class Groundhog
{
protected int number;
public Groundhog(){
}
public Groundhog(int number)
{
this.number = number;
}
@Override
public String toString()
{
return "Groundhog{" + "number=" + number + '}';
}
}
/**
* Description:新建一个类作为map的value
*/
public class Prediction
{
private boolean shadow=Math.random() > 0.5;
@Override
public String toString()
{
if (shadow) return "Six more weeks of Winter";
else return "Early Spring!";
}
}
/**
* Description:测试类
*/
public class SpringDetector
{
public static void detectSpring(Class grondHotClass) throws Exception{
Constructor constructor = grondHotClass.getConstructor(new Class[]{int.class});
Map map=new HashMap();
for (int i=0;i<10;i++){
map.put(constructor.newInstance(new Object[]{new Integer(i)}),new Prediction());
}
System.out.println("map="+map);
Groundhog groundhog=(Groundhog)constructor.newInstance(new Object[]{new Integer(3)});
System.out.println(groundhog);
if (map.containsKey(groundhog)) {//查找这个key是否存在
System.out.println((Prediction)map.get(groundhog));
}else {
System.out.println("key not find:"+groundhog);
}
}
public static void main(String[] args)
{
try {
detectSpring(Groundhog.class);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Description:首先新建一个类作为map中存储的对象并重写了hashCode()和equals()方法
*/
public class MPair implements Map.Entry,Comparable
{
private Object key,value;
public MPair(Object key,Object value)
{
this.key = key;
this.value=value;
}
@Override
public int compareTo(Object o)
{
return ((Comparable)key).compareTo(((MPair)o).key);
}
@Override
public Object getKey()
{
return key;
}
@Override
public Object getValue()
{
return value;
}
@Override
public int hashCode()
{
int result = key != null ? key.hashCode() : 0;
result = 31 * result + (value != null ? value.hashCode() : 0);
return result;
}
@Override
public boolean equals(Object o)
{
return key.equals(((MPair)o).key);
}
@Override
public Object setValue(Object v)
{
Object result=value;
this.value=v;
return result;
}
@Override
public String toString()
{
return "MPair{" + "key=" + key + ", value=" + value + '}';
}
public class SimpleHashMap extends AbstractMap
{
private static final int SZ=3;//定一个初始大小的哈希表容量
private LinkedList[] linkedLists=new LinkedList[SZ];//建一个hash数组,用linkedList实现
public Object put(Object key,Object value){
Object result=null;
int index=key.hashCode() % SZ;//对key的值做求模法求出index
if (index<0) index=-index;
if (linkedLists[index]==null) linkedLists[index]=new LinkedList();//如果这个index位置没有对象,就新建一个
LinkedList linkedList = linkedLists[index];//取出这个index的对象linkedList
MPair mPair = new MPair(key,value);//新建要存储的对象mPair
ListIterator listIterator = linkedList.listIterator();
boolean found =false;
while (listIterator.hasNext()){//遍历这个index位置的List,如果查找到跟之前一样的对象(根据equals来比较),则更新那个key对应的value
Object next = listIterator.next();
if (next.equals(mPair)){
result = ((MPair) next).getValue();
listIterator.set(mPair);//更新动作
found=true;
break;
}
}
if (!found) linkedLists[index].add(mPair);//如果没有找到这个对象,则在这index的List对象上新增一个元素。
return result;
}
public Object get(Object key){
int index = key.hashCode() % SZ;
if (index<0) index=-index;
if (linkedLists[index]==null) return null;
LinkedList linkedList = linkedLists[index];
MPair mPair=new MPair(key,null);//新建一个空的对象值,因为equals()的比较是看他们的key是否相等,而在List中的遍历对象的时候,是通过key来查找对象的。
ListIterator listIterator = linkedList.listIterator();
while (listIterator.hasNext()){
Object next = listIterator.next();
if (next.equals(mPair)) return ((MPair)next).getValue();//找到了这个key就返回这个value
}
return null;
}
@Override
public Set<Entry> entrySet()
{
Set set=new HashSet();
for (int i=0;i<linkedLists.length;i++){
if (linkedLists[i]==null) continue;
Iterator iterator = linkedLists[i].iterator();
while (iterator.hasNext()){
set.add(iterator.next());
}
}
return set;
}
public static void main(String[] args)
{
SimpleHashMap simpleHashMap=new SimpleHashMap();
simpleHashMap.put("1", "1");
simpleHashMap.put("2", "2");
simpleHashMap.put("3","3");
simpleHashMap.put("4","4");//这里有四个元素,其中key是1和key是4的index是一样的,所以index为1的List上面存了两个元素。
System.out.println(simpleHashMap);
Object o = simpleHashMap.get("1");
System.out.println(o);
Object o1 = simpleHashMap.get("4");
System.out.println(o1);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有