码迷,mamicode.com
首页 > 编程语言 > 详细

链表底层实现Java的Map(一)

时间:2019-03-08 22:11:24      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:void   val   link   return   get   empty   \n   链表   bre   

链表实现Map

public class LinkListMap<K,V> implements Map<K,V> {

    private class Node
    {
        public K key;

        public V value;

        public Node next;

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

            this.value = value;

            this.next = next;
        }

        public Node()
        {
            this(null,null,null);
        }

        public Node(K key)
        {
            this(key,null,null);
        }

        @Override
        public String toString()
        {
            return key.toString() + ":" + value.toString();
        }
    }

    private Node dummyHead;

    private int size;

    public LinkListMap()
    {
        dummyHead = new Node();
        size = 0;
    }

    private Node getNode(K key)
    {
        Node cur = dummyHead.next;
        while (cur != null)
        {
            if (cur.key.equals(key))
            {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public void add(K key, V value) {
        Node node = getNode(key);
        if (node == null)
        {
            dummyHead.next = new Node(key,value,dummyHead.next);
            size++;
        }else {
            node.value = value;
        }
    }

    @Override
    public V remove(K key) {
        Node prv = dummyHead;
        while (prv.next != null)
        {
            if (prv.next.key.equals(key))
            {
                break;
            }
            prv = prv.next;
        }

        if (prv.next != null)
        {
            Node delNode = prv.next;
            prv.next = delNode.next;
            delNode.next = null;
            return  delNode.value;
        }
        return null;
    }

    @Override
    public boolean contains(K key) {
        return getNode(key) != null;
    }

    @Override
    public V get(K key) {
        Node node = getNode(key);
        return node == null  ? null : node.value;
    }

    @Override
    public void set(K key, V newValue) {
        Node node = getNode(key);
        if (node == null)
        {
            throw new IllegalArgumentException("key is no exist.");
        }else {
            node.value = newValue;
        }
    }

    @Override
    public int getSize()
    {
        return size;
    }

    @Override
    public boolean isEmpty()
    {
        return size == 0;
    }

    @Override
    public String toString()
    {
        StringBuffer str = new StringBuffer("");
        Node node = dummyHead.next;
        while (node != null)
        {
            str.append(node + "\n");
            node = node.next;
        }
        return str.toString();
    }

}

 

链表底层实现Java的Map(一)

标签:void   val   link   return   get   empty   \n   链表   bre   

原文地址:https://www.cnblogs.com/dslx/p/10498451.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!