基于LinkedList手写hashMap1.7

定义map接口

package live.yanxiaohui.map;

/**
 * @Description 自定义map
 * @Author: yanxh<br>
 * @Date 2019/12/2 14:02<br>
 * @Version 1.0<br>
 */
public interface YXHMap<K,V> {

    /**
     * @Description 获取集合大小
     * @Author: yanxh<br>
     * @Date 2019/12/2 14:07<br>
     * @Version 1.0<br>
     */
    int size();

    /**
     * @Description 存储键值对
     * @Author: yanxh<br>
     * @Date 2019/12/2 14:07<br>
     * @Version 1.0<br>
     */
    V put(K k, V v);

    /**
     * @Description 根据key获取value
     * @Author: yanxh<br>
     * @Date 2019/12/2 14:07<br>
     * @Version 1.0<br>
     */
    V get(K k);

    /**
     * @Description 集中定义规范
     * @Author: yanxh<br>
     * @Date 2019/12/2 14:06<br>
     * @Version 1.0<br>
     */
    interface Entry<K, V>{
        /**
         * @Description 获取key
         * @Author: yanxh<br>
         * @Date 2019/12/2 14:06<br>
         * @Version 1.0<br>
         */
        K getKey();

        /**
         * @Description 获取value
         * @Author: yanxh<br>
         * @Date 2019/12/2 14:06<br>
         * @Version 1.0<br>
         */
        V getValue();
    }
}

 

定义hashmap

package live.yanxiaohui.map;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Description linkedList实现hashMap,查询时间复杂度O(1),即只查一次
 * @Author: yanxh<br>
 * @Date 2019/12/2 14:07<br>
 * @Version 1.0<br>
 */
public class YXHLinkedListHashMap<K ,V> implements YXHMap<K,V> {

    private LinkedList<Node<K, V>>[] tables = new LinkedList[100];

    @Override
    public int size() {
        int size = 0;
        for (LinkedList<Node<K, V>> list : tables){
            if(list == null){
                continue;
            }
            size += list.size();
        }
        return size;
    }

    @Override
    public V put(K k, V v) {

        Node<K ,V> node = new Node<K ,V>(k, v);
        /**
         * 允许key为null, 默认放在table首位
         */
        if(k == null){
            LinkedList<Node<K, V>> list = tables[0];
            if(list == null){
                list = new LinkedList<Node<K, V>>();
                list.add(node);
            }else{
                list.get(0).v = node.v;
            }
            // 放入数组中
            tables[0] = list;
            return v;
        }

        // 约定index的计算规则,key的hashCode对table长度取模
        int index = k.hashCode() % tables.length;

        // key的hashCode相等的node
        LinkedList<Node<K, V>> list = tables[index];
        if(list == null){
            list = new LinkedList<Node<K, V>>();
        }

        Node<K, V> oldNode = getNode(list, k);
        if(oldNode == null){
            list.add(node);
        }else{
            oldNode.v = node.v;
        }
        // 放入数组中
        tables[index] = list;
        return v;

    }

    /**
     * @Description 通过key和相同hashCode的nodes获取node节点
     * @Author: yanxh<br>
     * @Date 2019/12/2 15:31<br>
     * @Version 1.0<br>
     */
    private Node<K, V> getNode(LinkedList<Node<K, V>> nodes, K k){
        for (Node<K, V> node : nodes) {
            if(node.getKey().equals(k)){
                return node;
            }
        }
        return null;
    }

    @Override
    public V get(K k) {
        if(k == null){
            LinkedList<Node<K, V>> list = tables[0];
            if(list == null){
                return null;
            }
            Node<K, V> node = list.get(0);
            return node == null ? null : node.getValue();
        }

        // 根据约定获取下标
        int index = k.hashCode() % tables.length;
        LinkedList<Node<K, V>> list = tables[index];
        if(list == null){
            return null;
        }

        Node<K, V> node = getNode(list, k);
        return node == null ? null : node.getValue();
    }



    public class Node<K, V> implements Entry<K, V>{

        private K k;

        private V v;

        public Node(K k, V v) {
            this.k = k;
            this.v = v;
        }

        @Override
        public K getKey() {
            return k;
        }

        @Override
        public V getValue() {
            return v;
        }
    }
}

 

测试 

package live.yanxiaohui.test;

import live.yanxiaohui.map.YXHLinkedListHashMap;
import live.yanxiaohui.map.YXHMap;

/**
 * @Description TODO
 * @Author: yanxh<br>
 * @Date 2019/12/2 14:16<br>
 * @Version 1.0<br>
 */
public class TestArrayListHashMap {
    public static void main(String[] args){

        /*YXHMap<String, String> map = new YXHArrayListHashMap<String, String>();
        map.put("name", "yxh");
        map.put("name", "map");
        System.out.println(map.get("name"));*/

        YXHMap<Object, String> map = new YXHLinkedListHashMap<>();
        map.put("name", "sbdy");
        System.out.println(map.get("name"));

        System.out.println(map.get(null));

        map.put(null, "塞北的鱼");
        System.out.println(map.get(null));

        map.put(null, "塞北的鱼2");
        System.out.println(map.get(null));

        map.put("a", "帝都的雁");
        System.out.println(map.get("a"));

        map.put("a", "帝都的雁aa");
        System.out.println(map.get("a"));

        map.put(97, "帝都的雁2");
        System.out.println(map.get(97));

        /*System.out.println(map.get(99));*/
        System.out.println(map.size());
    }
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值