手撕 hashMap 和 hashSet java代码(拉链法)

哈希表的本质就是一个数组,但是不管是对这个数组进行插入删除和查找操作,其时间复杂度接近于O(1);但是这个数组中不会出现重复的元素,比如一个哈希表中已经有了元素1和2,当我们再往里面插入一个元素1的时候,就插不进去了。

就是根据哈希函数得到哈希值然后找到下标然后查看这个位置的是否有元素,没有元素则直接插入,有元素则比较是否一样,一样的话就是插入失败,(拉链法)。

以 <key, value> 值得map

package myHash;

import java.util.Objects;

class TNode{
    public String key;
    public String val;
    public TNode next;


    public TNode(String key, String val) {
        this.key = key;
        this.val = val;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        TNode tNode = (TNode) o;
        return Objects.equals(key, tNode.key) &&
                Objects.equals(val, tNode.val) &&
                Objects.equals(next, tNode.next);
    }

    @Override
    public int hashCode() {
        return Objects.hash(key, val, next);
    }
}


public class MyHashMap {
    public TNode[] array = new TNode[101];
    public int size;

    public boolean put(String key, String val) {
        int hasValues = key.hashCode();
        int index = hasValues/array.length;

        TNode tnode = array[index];
        if (tnode == null) {
            array[index] = new TNode(key, val);
            size++;
            return true;
        }

        TNode cur = new TNode(key, val);
        if (contains(cur)) {
            return false;
        }

        cur.next = array[index];
        array[index] = cur;
        return true;

    }

    public boolean contains(TNode tNode) {
        int hasValues = tNode.key.hashCode();
        int index = hasValues/array.length;

        TNode cur = array[index];
        if (cur == null) {
            return false;
        }

        while (cur != null) {
            if (cur.key.equals(tNode.key)) {
                return true;
            }
            cur = cur.next;
        }

        return false;
    }

    public boolean remove(TNode tNode) {
        int hasValues = tNode.key.hashCode();
        int index = hasValues/array.length;

        TNode cur = array[index];
        if (cur == null) {
            return false;
        }

        TNode prev = null;//设置一个前驱节点
        while (cur != null) {
            if (cur.key.equals(tNode.key)) {
                if (prev == null) {
                    //头节点是要移除的节点
                    array[index] = array[index].next;
                } else {
                    prev.next = cur.next;
                }
                size--;
                return true;
            }
            prev = cur;
            cur = cur.next;
        }
        return false;
    }
    
}

hashSet

package myHash;

import java.util.Objects;

class TNode{
    public String key;
    public String val;
    public TNode next;


    public TNode(String key, String val) {
        this.key = key;
        this.val = val;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        TNode tNode = (TNode) o;
        return Objects.equals(key, tNode.key) &&
                Objects.equals(val, tNode.val) &&
                Objects.equals(next, tNode.next);
    }

    @Override
    public int hashCode() {
        return Objects.hash(key, val, next);
    }
}


public class MyHashMap {
    public TNode[] array = new TNode[101];
    public int size;

    public boolean put(String key, String val) {
        int hasValues = key.hashCode();
        int index = hasValues/array.length;

        TNode tnode = array[index];
        if (tnode == null) {
            array[index] = new TNode(key, val);
            size++;
            return true;
        }

        TNode cur = new TNode(key, val);
        if (contains(cur)) {
            return false;
        }

        cur.next = array[index];
        array[index] = cur;
        return true;

    }

    public boolean contains(TNode tNode) {
        int hasValues = tNode.key.hashCode();
        int index = hasValues/array.length;

        TNode cur = array[index];
        if (cur == null) {
            return false;
        }

        while (cur != null) {
            if (cur.key.equals(tNode.key)) {
                return true;
            }
            cur = cur.next;
        }

        return false;
    }

    public boolean remove(TNode tNode) {
        int hasValues = tNode.key.hashCode();
        int index = hasValues/array.length;

        TNode cur = array[index];
        if (cur == null) {
            return false;
        }

        TNode prev = null;//设置一个前驱节点
        while (cur != null) {
            if (cur.key.equals(tNode.key)) {
                if (prev == null) {
                    //头节点是要移除的节点
                    array[index] = array[index].next;
                } else {
                    prev.next = cur.next;
                }
                size--;
                return true;
            }
            prev = cur;
            cur = cur.next;
        }
        return false;
    }
    
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值