HashMap1.7

HashMap1.7

  • 思想:就是数组加链表实现,当数据比较少的时候,效率还可以,但是数据量大的时候,明显效率低,出现就出现在 链表的缺陷。
  • 这里我就简单实现下增加即可。
package JDK17HashMap;

import static java.lang.Math.pow;


/**
 *
 * HashMap 1.7
 *
 */
public class WZFHashMap<K extends Comparable<K>,V>
{
    //数组的长度 默认为10
    private static final int ARRNUM = 10;

    //用户自定义容量
    private int arrsNum =0;

    //Node 数组
    private Node nodes[];


    /**
     *
     * @param arrsCapacity  数组容量
     */
    public WZFHashMap(int arrsCapacity)
    {
        this.nodes = new Node[arrsCapacity];
        this.arrsNum=arrsCapacity;
    }

    /**
     * 默认是初始化 10容量的数组
     */
    public WZFHashMap()
    {
        this.nodes = new Node[ARRNUM];
    }


    /**
     * 插入 节点操作
     * @param key 键
     * @param value 值
     */
    public void insertNode(K key,V value)
    {
        if(this.nodes == null)
        {
            throw  new RuntimeException ("初始化失败!");
        }

        Node node = new Node (key, value,null);
        if(node == null)
        {
            throw  new RuntimeException ("Node节点初始化失败!");
        }


        //如果key为空,默认存放在 0位置上
        if(key == null)
        {
            selectInsertPosition (0,node);
        }
        /**
         * 这后面就是key 不为null的时候
         */
        else
        {
            int hash = hash (key);
            selectInsertPosition (hash,node);
        }
    }

    /**
     *
     * @param hash 计算出来的hash值
     * @param node 待插入节点
     */
    private void selectInsertPosition(int hash,Node node)
    {
        Node p = nodes[hash];
        if(p == null)
        {
            nodes[hash]=node;
            return;
        }
        Node prior = null;
        while (p!=null)
        {
            prior = p;
            p=p.next;
        }
        //此时p指向待插入位置  prior指向前驱
        prior.next = node;
    }


    /**
     * 获取数组长度
     * @return
     */
    private int getArrnum()
    {
        return this.arrsNum == 0 ? ARRNUM : arrsNum;
    }

    /**
     * 计算hash 值
     * @param key  键值
     * @return hash值
     */
    private int hash(K key)
    {
        int num;
        //代表用户 自定义了
        if(arrsNum != 0)
        {
            num =  key.hashCode () % arrsNum;
        }
        else
        {
            num = key.hashCode () % ARRNUM;
        }
        return num;
    }


    /**
     * 打印
     */
    public void printHashMap()
    {
        for(int i =0;i<getArrnum ();i++)
        {
            System.out.println ("**************************");
            Node p =nodes[i];
            while (p != null)
            {
                System.out.println ("key:"+p.key+"  value:"+p.value);
                p=p.next;
            }
            System.out.println ("**************************");
        }
    }





    /**
     * 结点
     */
    static class Node<K extends Comparable<K>,V>
    {
        //key
        private K key;
        //value
        private V value;

        //下一个节点
        private Node next;

        public Node(K key, V value,Node node)
        {
            this.key = key;
            this.value = value;
            this.next = node;
        }

        public Node()
        {
        }

        public Node getNext()
        {
            return next;
        }

        public void setNext(Node next)
        {
            this.next = next;
        }

        public K getKey()
        {
            return key;
        }

        public void setKey(K key)
        {
            this.key = key;
        }

        public V getValue()
        {
            return value;
        }

        public void setValue(V value)
        {
            this.value = value;
        }


    }
}

测试类

package JDK17HashMap;

import java.util.HashMap;

public class HashTest
{
    public static void main(String[] args)
    {
        WZFHashMap<Integer, String> map = new WZFHashMap<> (3);
        map.insertNode (2,"美国");
        map.insertNode (1,"中国");
        map.insertNode (3,"英国");
        map.insertNode (2,"俄罗斯");
        map.insertNode (2,"日本");
        map.insertNode (1,"太平洋");
        map.insertNode (3,"法国");
        map.insertNode (3,"你好");

        map.printHashMap ();

    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值