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;inext; } }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;iif(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 if(e.key.equals(key)){ return e.value; } } } return null; } public static void main(String[] args) { } }