zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    (转)深入浅出JWT(JSON Web token)
    Node.js Koa2开发微信小程序服务端
    天翼宽带家庭网关用户:useradmin,nE7jA%5m 这个是中国电信的超级密码
    微信小程序picker重写,精确到时分秒
    Vue props中Object和Array设置默认值
    GreenDao学习
    Android注解支持(Support Annotations) (转)
    异常:Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details.
    精通 Android Data Binding
    Android BroadcastReceiver介绍 (转)
  • 原文地址:https://www.cnblogs.com/brant/p/6231216.html
Copyright © 2011-2022 走看看