数据结构HashMap

数据结构HashMap

作为一个大一新生自己写的HashMap

因为HashMap是Java比较重要的一个集合,通过我对数据结构的学习,也为了跟好的了解JavaHashMap源码,通过链表+数组自己做了一个简易的HashMap,可能有很多问题希望不要喷,毕竟我终是一个大一新生

public class HashListMap{


    private static HashListMap hashListMap=null;

    public static synchronized HashListMap hashListMap(int SizeNode){
        if(hashListMap==null){
            hashListMap=new HashListMap(SizeNode);
        }
        return hashListMap;
    }

    public class Node{
        String key;
        String value;
        Node next;
        public Node(String key,String value){
            this.key=key;
            this.value=value;
        }
    }

    private int SizeNode=0;

    //传入你要设置的链表长度
    public HashListMap(int SizeNode){
        this.SizeNode=SizeNode;
        head=new Node[SizeNode];
        tail=new Node[SizeNode];
    }

    Node[] head;
    Node[] tail;


    //计算hash值

    public int hash(String key){
        return key==null?0:key.hashCode()&(SizeNode-1);
    }

    public void insert(String key,String Value){
        Node node=new Node(key,Value);
        int hash=this.hash(key);
        if(head[hash]==null&&tail[hash]==null){
            tail[hash]=node;
            head[hash]=tail[hash];
        }else{
            tail[hash].next=node;
            tail[hash]=node;
        }
    }

    public String Get(String key){
        int hash=this.hash(key);
        Node cur=head[hash];
       while (cur!=null&&cur.key!=key){
           cur=cur.next;
       }
       return cur==null?null:cur.value;
    }

    public void GetNode(){
        for(int i=0;i<SizeNode;i++){
            Node cur=head[i];
            while (cur!=null){
                System.out.println(cur.key);
                System.out.println(cur.value);
                cur=cur.next;
            }
        }
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值