ArrayList和LinkList和Map源码实现

package com.bootdo.common.config.collection.test.collectionmap.arraylist;

/**
 * @ClassName :   MyArrayList
 * @Description: 模仿实现底层的ArrayList的一些方法的实现
 * @Author: 13394
 * @CreateDate: 2018/10/19 22:46
 * @Version: 1.0
 */
public class MyArrayList {

    private static final Object[] EMPTY_ELEMENTDATA = {};
    private Object[] elementData;
    private int size;

    public MyArrayList() {
        this(10); //初始化调用一个构造方法
    }

    public MyArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: " +
                    initialCapacity);
        }
    }

    public boolean add(Object obj) {
        ensureCapacityInternal();//数组的扩容
        elementData[size++] = obj;
        return true;
    }

    private void rangeCheckForAdd(int index) {
        if (index > size || index < 0) {
            throw new IndexOutOfBoundsException();
        }
    }

    private String outOfBoundsMsg(int index) {
        return "index:" + index + "size:" + size;
    }

    private void ensureCapacityInternal() {
        if (size == elementData.length) {
            Object[] newArray = new Object[size * 2 + 1];
            System.arraycopy(elementData, 0, newArray, 0, elementData.length);
            elementData = newArray;
        }
    }

    public void add(int index, Object obj) {
        rangeCheckForAdd(index); //判断数组是否越界
        ensureCapacityInternal(); //数组的扩容
        System.arraycopy(elementData, index, elementData, index + 1, size - index);
        elementData[index] = obj;
        size++;
    }

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

    public Object get(int index) {
        rangeCheck(index);
        return elementData[index];
    }

    private void rangeCheck(int index) {
        if (index >= size)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }


    public static void main(String[] args) {
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.add("ads");
        myArrayList.add(1, "13132");
        //System.out.println(myArrayList.get(4));
        System.out.println(myArrayList.get(1));


    }

}

LinkList

package com.bootdo.common.config.collection.test.collectionmap.linkedlist;

import java.util.LinkedList;
import java.util.Map;

/**
 * @ClassName :   MyLinkList
 * @Description: TODO
 * @Author: 13394
 * @CreateDate: 2018/10/20 19:08
 * @Version: 1.0
 */
public class MyLinkList {
    private Node first;
    private Node last;
    private int size;

    public void add(Object obj) {
        Node node = new Node();
        if (first == null) {
            node.setPrevious(null);
            node.setObj(obj);
            node.setNext(null);
            first = node;
            last = node;
        } else {
            node.setPrevious(last);
            node.setObj(obj);
            node.setNext(null);
            last = node;
        }
        size++;
    }

    public int size() {
        return size;
    }

    private String outOfBoundsMsg(int index) {
        return "index:" + index + " " + "size:" + size;
    }


    private void rangeCheck(int index) {
        if (index > size) {
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
        }
    }

    public Object get(int index) {
        rangeCheck(index);
        Node temp = null;
        if (first != null) {
            temp = first;
            for (int i = 0; i < index; i++) {
                temp = temp.next;
            }
        }
        return temp.obj;
    }

    public Node node(int index){
        Node temp = null;
        if(first!=null){
            if (index < (size >> 1)) {
                temp = first;
                for(int i=0;i<index;i++){
                    temp = temp.next;
                }
            }else{
                temp = last;
                for (int i = size - 1; i > index; i--){
                    temp = temp.previous;
                }
            }

        }
//    LinkedList l;
        return temp;
    }


    public void remove(int index){
        Node temp = node(index);

        if(temp!=null){
            Node up = temp.previous;
            Node down = temp.next;
            up.next = down;
            down.previous = up;
            size--;
        }

    }

    public void add(int index,Object obj){
        Node temp = node(index);

        Node newNode = new Node();
        newNode.obj = obj;

        if(temp!=null){
            Node up = temp.previous;
            up.next = newNode;
            newNode.previous = up;

            newNode.next = temp;
            temp.previous = newNode;

            size++;
        }
    }

    public static void main(String[] args) {
        MyLinkList myLinkList = new MyLinkList();
        myLinkList.add("ada");
        myLinkList.add("dasda");
        System.out.println(myLinkList.size());
        System.out.println(myLinkList.get(5));
    }
}
package com.bootdo.common.config.collection.test.collectionmap.linkedlist;

/**
 * @ClassName :   Node
 * @Description:
 * @Author: 13394
 * @CreateDate: 2018/10/20 19:14
 * @Version: 1.0
 */
public class Node {
     Node previous; //上一个节点
     Object obj;
     Node next;  //下一个节点
    public Node(){


    }

    public Node(Node previous, Object obj, Node next) {
        this.previous = previous;
        this.obj = obj;
        this.next = next;
    }

    public Node getPrevious() {
        return previous;
    }

    public void setPrevious(Node previous) {
        this.previous = previous;
    }

    public Object getObj() {
        return obj;
    }

    public void setObj(Object obj) {
        this.obj = obj;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

Map

package com.bootdo.common.config.collection.test.collectionmap.map;

import java.util.LinkedList;

/**
 * @ClassName :   MyMap002
 * @Description: TODO
 * @Author: 13394
 * @CreateDate: 2018/10/21 18:49
 * @Version: 1.0
 */
public class MyMap002 {
    LinkedList[]  arr  = new LinkedList[9]; //Map的底层结构就是:数组+链表!
    int size;

    public void put(Object key,Object value){
        Entry  e = new Entry(key,value);

        int hash = key.hashCode();
        hash = hash<0?-hash:hash;

        int a = hash%arr.length;
        if(arr[a]==null){
            LinkedList list = new LinkedList();
            arr[a] = list;
            list.add(e);
        }else{
            LinkedList list = arr[a];
            for(int i=0;i<list.size();i++){
               Entry e2 = (Entry) list.get(i);
                if(e2.key.equals(key)){
                    e2.value = value;  //键值重复直接覆盖!
                    return;
                }
            }

            arr[a].add(e);
        }
        //a:1000-->1   b:10000-->13
    }

    public Object get(Object key){
        int a = key.hashCode()%arr.length;
        if(arr[a]!=null){
            LinkedList list = arr[a];
            for(int i=0;i<list.size();i++){
                Entry e = (Entry) list.get(i);
                if(e.key.equals(key)){
                    return e.value;
                }
            }
        }

        return null;
    }

    public static void main(String[] args) {

    }
}

 

转载于:https://my.oschina.net/u/3653755/blog/2250283

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值