Java集合篇五:HashMap

1.HasMap 自定义基础版

package com.test.collection;


/**
 * 自定义实现Map功能
 * map :存放键值对,根据键对象找对应的值对象
 * @author chenx
 *
 */
public class MyMap001 {
    Entry[] arr=new Entry[999];
    int size;
    
    public void put(Object key,Object value){
        Entry e=new Entry(key,value);
        
        //解决键重复的处理,后面的直接覆盖
        for(int i=0;i<size;i++){
            if(arr[i].key.equals(key)){
                arr[i].value =value;
                return;
            }
        }
        arr[size++]=e;
    }
    
    public Object get(Object key){
        for(int i=0;i<size;i++){
            if(arr[i].key.equals(key)){
                return arr[i].value;
            }
        }
        return null;
    }
    public boolean containsKey(Object key){
        for(int i=0;i<size;i++){
            if(arr[i].key.equals(key)){
                return true;
            }
        }
        return false;
    }
    public int size(){
        return size;
    }
    public static void main(String[] args) {
        MyMap001 map=new MyMap001();
        map.put("a", "张三");
        map.put("a", "李四");
        map.put("c", "王五");
        //map.remove(2);
        System.out.println(map.get("a"));
        System.out.println(map.size());
        
        
    }

}

class Entry{
    Object key;
    Object value;
    public Entry(Object key, Object value) {
        super();
        this.key = key;
        this.value = value;
    }
}

2.HasMap 自定义升级版

package com.test.collection;

import java.util.LinkedList;


/**
 * 自定义实现Map功能(升级版)
 * map :存放键值对,根据键对象找对应的值对象
 * 
 * 1.提升查询效率,避免MyMap002中的循环遍历: 数组+链表
 * 
 * hashMap:底层实现(数组+链表),链表中放对象,对象中存key,value
 * 
 * @author chenx
 *
 */
public class MyMap002 {
    LinkedList[] arr=new LinkedList[999];
    int size;
    
    public void put(Object key,Object value){
        Entry2 e=new Entry2(key,value);
        int has=key.hashCode();
        has = has<0?-has:has;
        int a =has%arr.length;
        if(arr[a] ==null){
            LinkedList list =new LinkedList();
            arr[a] =list;
            list.add(e);
        }else{
            
            LinkedList list=arr[a];
            for(int i=0;i<list.size();i++){
                Entry2 e1=(Entry2)list.get(i);
                if(e1.key.equals(key)){
                    e1.value = value;//重复的进行覆盖
                    return;
                }
            }
            arr[a].add(e);
        }
    }
    
    public Object get(Object key){
        int has=key.hashCode();
        has = has<0?-has:has;
        int a =has%arr.length;
        if(arr[a] !=null){
            LinkedList list=arr[a];
            for(int i=0;i<list.size();i++){
                Entry2 e=(Entry2)list.get(i);
                if(e.key.equals(key)){
                    return e.value;
                }
            }
        }
        return null;
    }
    
    public static void main(String[] args) {
        MyMap002 map=new MyMap002();
        map.put("a", "张三");
        map.put("a", "李四");
        map.put("c", "王五");
        //map.remove(2);
        System.out.println(map.get("a"));
        //System.out.println(map.size());
        
        
    }

}

class Entry2{
    Object key;
    Object value;
    public Entry2(Object key, Object value) {
        super();
        this.key = key;
        this.value = value;
    }
}
原文地址:https://www.cnblogs.com/brant/p/6231216.html