• java——链表映射 LinkedListMap


    好像也不是基于链表来实现的,而是采用与链表类似的节点形式重新定义了一个节点内部类,以此来实现映射

    public class LinkedListMap<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(K key) {
                this(key, null, null);
            }
            public Node() {
                this(null, null, null);
            }
            
            @Override
            public String toString() {
                return key.toString() + " : " + value.toString();
            }
        }
        
        private Node dummyHead;
        private int size;
    
        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) {
            //key具有唯一性
            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 prev = dummyHead;
            while(prev.next != null) {
                if(prev.next.key.equals(key)) {
                    break;
                }
                prev = prev.next;
            }
            if(prev.next != null) {
                Node cur = prev.next;
                prev.next = cur.next;
                cur.next = null;
                size --;
                return cur.value;
            }
            return null;
        }
    
        @Override
        public boolean contains(K key) {
            // TODO Auto-generated method stub
            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 + "doesn't exist!");
            node.value = newValue;
        }
    
        @Override
        public int getSize() {
            // TODO Auto-generated method stub
            return size;
        }
    
        @Override
        public boolean inEmpty() {
            // TODO Auto-generated method stub
            return size == 0;
        }
        
        
    }
  • 相关阅读:
    Windows JScript 在 游览器 中运行 调试 Shell 文件系统
    autohotkey 符号链接 软连接 symbolink
    软链接 硬链接 测试
    SolidWorks 修改 基准面 标准坐标系
    手机 路径 WebDAV 映射 驱动器
    Win10上手机路径
    explorer 命令行
    单位公司 网络 封锁 屏蔽 深信 AC
    cobbler自动化部署原理篇
    Docker四种网络模式
  • 原文地址:https://www.cnblogs.com/gaoquanquan/p/9866477.html
Copyright © 2020-2023  润新知