首页 > 编程语言 > 详细

超级简单的数组加单链表实现Map

时间:2019-09-17 01:04:43      阅读:189      评论:0      收藏:0      [点我收藏+]
/**
 * 超级简单的数组加单链表实现Map
 * @author jlj
 *
 */
public class MyHashMap {
    
    public MyList[] lists;
    public int initSize = 10;
    
    public MyHashMap(){
        lists = new MyList[initSize];
    }
    
    public void addNode(Node node){
        int id = node.id;
        MyList list = lists[id % initSize];
        
        if(list == null){
            MyList l = new MyList();
            l.headNode = node;
            lists[id % 10] = l;
        } else {
            Node headNode = list.headNode;
            while(headNode.next != null){
                headNode = headNode.next;
            }
            headNode.next = node;
        }
    }
    
    public static void main(String[] args) {
        MyHashMap map = new MyHashMap();
        map.addNode(new Node(1));
        map.addNode(new Node(0));
        map.addNode(new Node(3));

        map.addNode(new Node(10));
        System.out.println(map.lists.length);
        System.out.println(map.lists[0]);
    }
    
    
}


class Node{
    public int id;
    public Node next;
    public Node(int id) {
        super();
        this.id = id;
    }
    @Override
    public String toString() {
        return "Node [id=" + id + ", next=" + next + "]";
    }
    
    
    
}

class MyList{
    public Node headNode;

    @Override
    public String toString() {
        return "MyList [headNode=" + headNode + "]";
    }
    
    
}

超级简单的数组加单链表实现Map

原文:https://www.cnblogs.com/theone67/p/11531014.html

(1)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!