// Get Element public E getFirst(){} public E getLast(){} public E get(int index){} public E peek(){} public E element(){} public E peekFirst(){} public E peekLast(){} public E pollFirst(){} public E pollLast(){}
// Remove Element public E pop(){} public E poll(){} public E removeFirst(){} public E removeLast(){} publicbooleanremove(Object o){} public E remove(int index){} public E remove(){} publicbooleanremoveFirstOccurrence(Object o){} publicbooleanremoveLastOccurrence(Object o){}
// Add Element publicvoidaddFirst(E e){} publicvoidaddLast(E e){} publicbooleanadd(E e){} publicvoidadd(int index, E element){} publicbooleanaddAll(Collection<? extends E> c){} publicbooleanaddAll(int index, Collection<? extends E> c){} public E set(int index, E element){}
/** * 获取第一个节点,如果集合为empty,则抛出NoSuchElementException */ public E getFirst(){ final Node<E> f = first; if (f == null) thrownew NoSuchElementException(); return f.item; }
/** * 获取最后一个节点,如果List为null,则抛出NoSuchElementException */ public E getLast(){ final Node<E> l = last; if (l == null) thrownew NoSuchElementException(); return l.item; }
/** * 返回指定下标的元素 */ public E get(int index){ //检查元素下标是否合法 checkElementIndex(index); return node(index).item; }
/** * 检索返回第一个元素,但不删除,与element()方法不同,如果元素为null,则返回null,不抛出异常 */ public E peek(){ final Node<E> f = first; return (f == null) ? null : f.item; }
/** * 检索返回第一个元素,但不删除,如果List为null.抛出 NoSuchElementException */ public E element(){ return getFirst(); }
/** * 检索返回第一个元素,但不删除,若List为empty,则返回null */ public E peekFirst(){ final Node<E> f = first; return (f == null) ? null : f.item; }
/** * 检索返回最后一个元素,但不删除,若List为empty,则返回null */ public E peekLast(){ final Node<E> l = last; return (l == null) ? null : l.item; }
/** * 检索返回第一个元素,但不删除,如果List为empty,则返回null */ public E pollFirst(){ final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); }
/** * 检索返回最后一个元素,但不删除,如果List为empty,则返回null */ public E pollLast(){ final Node<E> l = last; return (l == null) ? null : unlinkLast(l); }
/** * 删除并返回第一个元素,等同于removeFirst(). */ public E pop(){ return removeFirst(); }
/** * 删除并返回第一个元素。如果List为null,则返回null */ public E poll(){ final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); }
/** * 删除第一个元素,并返回数据,如果List为空,则抛出NoSuchElementException */ public E removeFirst(){ final Node<E> f = first; if (f == null) thrownew NoSuchElementException(); return unlinkFirst(f); }
/** * 删除最后一个元素,并返回数据,如果List为空,则抛出NoSuchElementException */ public E removeLast(){ final Node<E> l = last; if (l == null) thrownew NoSuchElementException(); return unlinkLast(l); }
/** * 删除第一个符合要求的元素,如果不存在,则不进行任何改变。即:删除符合的元素中下标最小的那个。 * 分为null 和 not null。返回是否包含指定值(true和false) */ publicbooleanremove(Object o){ if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) { unlink(x); returntrue; } } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); returntrue; } } } returnfalse; }
/** * 移除并返回指定下标的元素(后面的元素全部左移,对于链表来说,无需手动移动)。 * 如果下标不合法,则抛出 IndexOutOfBoundsException */ public E remove(int index){ checkElementIndex(index); return unlink(node(index)); }
/** * 检索返回第一个元素,并移除,如果List为null,则抛出NoSuchElementException */ public E remove(){ return removeFirst(); }
/** * 移除最后一个命中的元素,(从尾遍历到头,即倒序遍历),若不包含,则不改变 */ publicbooleanremoveLastOccurrence(Object o){ if (o == null) { for (Node<E> x = last; x != null; x = x.prev) { if (x.item == null) { unlink(x); returntrue; } } } else { for (Node<E> x = last; x != null; x = x.prev) { if (o.equals(x.item)) { unlink(x); returntrue; } } } returnfalse; }
Object[] a = c.toArray(); int numNew = a.length; if (numNew == 0) //给定集合为empty,则返回false returnfalse;
//分为添加到链表最后,和添加到链表中 Node<E> pred, succ; if (index == size) { succ = null; pred = last; } else { succ = node(index); pred = succ.prev; }
for (Object o : a) { @SuppressWarnings("unchecked") E e = (E) o; Node<E> newNode = new Node<>(pred, e, null); if (pred == null) first = newNode; else pred.next = newNode; pred = newNode; }
/** * 将元素替换指定下标的元素。如果元素不合法,则抛出IndexOutOfBoundsException。 */ public E set(int index, E element){ checkElementIndex(index); Node<E> x = node(index); E oldVal = x.item; x.item = element; return oldVal; }
/** * 查找第一个符合的元素的下标(使用顺序遍历),如果没有,则返回-1。 * 即:返回所有符合的元素的最小下标。 */ publicintindexOf(Object o){ int index = 0; //判断当前元素是否为空(这里可以看出,LinkedList是可以存储null的) if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) return index; index++; } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) return index; index++; } } //如果没有找到,则返回-1 return -1; }
/** * 查找最后一个符合的元素的下标(使用倒序遍历),如果没有,则返回-1 * 即:返回符合要求的所有元素的最大下标。 */ publicintlastIndexOf(Object o){ int index = size; if (o == null) { for (Node<E> x = last; x != null; x = x.prev) { index--; if (x.item == null) return index; } } else { for (Node<E> x = last; x != null; x = x.prev) { index--; if (o.equals(x.item)) return index; } } return -1; }
/** * 移除集合中的所有元素。 */ publicvoidclear(){ for (Node<E> x = first; x != null; ) { Node<E> next = x.next; x.item = null; x.next = null; x.prev = null; x = next; } first = last = null; size = 0; modCount++; }
/** * 将链表转化为数组 */ public Object[] toArray() { Object[] result = new Object[size]; int i = 0; for (Node<E> x = first; x != null; x = x.next) result[i++] = x.item; return result; }
/** * 将集合填充到给定数据中,如果给定的数组大小小于集合元素的个数,则全部替换。 * 如果数组元素大于集合元素个数,则将集合大小的下标的元素设置为null, * 如果给定数据为null,则抛出NullPointerException * * 集合为:{"A","B","C","D"}. * 若数组为:{"AA","BB","CC","DD","EE","FF"}, ==> {"A","B","C","D",null,"FF"} * 若数组为:{"AA","BB","CC"} 或 {"AA","BB","CC","DD"} ==> {"A","B","C","D"} * */ @SuppressWarnings("unchecked") public <T> T[] toArray(T[] a) { //如果数据的长度小于当前集合元素的个数,创建一个size大小的数组 if (a.length < size) a = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size); int i = 0; Object[] result = a; for (Node<E> x = first; x != null; x = x.next) result[i++] = x.item;
@Test publicvoidtest(){ ArrayList<Integer> list = new ArrayList<>(); for (int i = 0; i < 100000; i++) { list.add(i); }
long start1 = System.currentTimeMillis(); for (int i = 0; i < list.size(); i++) { list.get(i); } long end1 = System.currentTimeMillis(); System.out.println("普通for:"+(end1 - start1));
long start2 = System.currentTimeMillis(); for (Integer i : list) {} long end2 = System.currentTimeMillis(); System.out.println("增加for:"+(end2 - start2));
long start3 = System.currentTimeMillis(); ListIterator<Integer> listIterator = list.listIterator(); while(listIterator.hasNext()){ listIterator.next(); } long end3 = System.currentTimeMillis(); System.out.println("ListIterator:"+(end3 - start3));
@Test publicvoidtest(){ LinkedList<Integer> linkedList = new LinkedList<>(); for (int i = 0; i < 100000; i++) { linkedList.add(i); }
//使用普通for循环遍历 long startTime = System.currentTimeMillis(); for (int i = 0; i < linkedList.size(); i++) { linkedList.get(i); } long endTime = System.currentTimeMillis(); System.out.println("普通for循环:"+(endTime-startTime));
//使用增强for循环遍历 long start1 = System.currentTimeMillis(); for (Integer i : linkedList) {} long end1 = System.currentTimeMillis(); System.out.println("增加for循环:"+(end1 - start1));
//使用ListIterator ListIterator<Integer> iterator = linkedList.listIterator(); long start = System.currentTimeMillis(); while(iterator.hasNext()){ iterator.next(); } long end = System.currentTimeMillis(); System.out.println("使用ListIterator遍历:"+(end - start));
Iterator<Integer> iter = linkedList.iterator(); long start2 = System.currentTimeMillis(); while(iter.hasNext()){ iter.next(); } long end2 = System.currentTimeMillis(); System.out.println("使用iterator:"+(end2 - start2)); } //测试结果 /** 普通for循环:3807 增加for循环:2 使用ListIterator遍历:1 使用iterator:1 */