集合-LinkList

参考:http://www.cnblogs.com/skywang12345/p/3308807.html

Consumer.class   消费者接口

参考:https://www.jianshu.com/p/63771441ba31

           https://blog.csdn.net/qq_28410283/article/details/80618456

public class ConsumerTest {
    public static void main(String[] args) {
        //testConsumer();
        testAndThen();
    }
    
    public static void testConsumer(){
        Consumer<Integer> square=x->System.out.println("print square: "+x*x);
        square.accept(2);
    }
    
    public static void testAndThen() {
        Consumer<Integer> consumer1=x->System.out.println("first x :"+x);
        Consumer<Integer> consumer2=x->{
            System.out.println("second x: "+x);
            throw new NullPointerException("throw exception test");
        };
        Consumer<Integer> consumer3=x->System.out.println("third x: "+x);
        consumer1.andThen(consumer2).andThen(consumer3).accept(1);
    }
}

 

函数式编程与lambda表达式:

    https://www.cnblogs.com/snowInPluto/p/5981400.html

    https://www.cnblogs.com/Dorae/p/7769868.html

    https://www.cnblogs.com/CarpenterLee/p/6729368.html

LinkList.class       public boolean addAll(int index, Collection<? extends E> c) 

    public boolean addAll(int index, Collection<? extends E> c) { 将一个集合c插入到链表中,index的位置
        checkPositionIndex(index);
        Object[] a = c.toArray();
        int numNew = a.length;
        if (numNew == 0)
            return false;

        Node<E> pred, succ;
        if (index == size) { 若是插入到最后一个元素的后面  
            succ = null;     这个元素指向null
            pred = last;     这个元素的前向指针指向之前的最后一个元素
        } else {
            succ = node(index);  这个元素后向指针指向原来index处的元素
            pred = succ.prev;    这个元素前向指针指向原来index处元素的前一个元素
        }

        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; 往后移一位
        }

        if (succ == null) {
            last = pred;
        } else {
            pred.next = succ;
            succ.prev = pred;
        }

        size += numNew;
        modCount++;
        return true;
    }
    Node<E> node(int index) {  这里采用二分法去查找 链表上第index个元素
        // assert isElementIndex(index);
        if (index < (size >> 1)) {
            Node<E> x = first;
            for (int i = 0; i < index; i++) 从链表第一个元素开始往后查找
                x = x.next;
            return x;
        } else {
            Node<E> x = last;
            for (int i = size - 1; i > index; i--)  从链表最后一个往前查找
                x = x.prev;
            return x;
        }
    }
每一个链表节点的数据结构:
    private static class Node<E> {
        E item; 当前元素
        Node<E> next;  下一个元素
        Node<E> prev;  上一个元素

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element; 
            this.next = next;
            this.prev = prev;
        }
    }
    private E unlinkFirst(Node<E> f) {  将第一个元素从链表的关联中去除 
        // assert f == first && f != null;
        final E element = f.item;
        final Node<E> next = f.next;
        f.item = null;
        f.next = null; // help GC
        first = next;
        if (next == null)
            last = null;
        else
            next.prev = null;
        size--;
        modCount++;
        return element;
    }
    /**
     * Unlinks non-null last node l.
     */
    private E unlinkLast(Node<E> l) {  将最后一个元素从链表的关联中去除
        // assert l == last && l != null;
        final E element = l.item;
        final Node<E> prev = l.prev;
        l.item = null;
        l.prev = null; // help GC
        last = prev;
        if (prev == null)
            first = null;
        else
            prev.next = null;
        size--;
        modCount++;
        return element;
    }
    private void linkFirst(E e) {  创建一个新的节点,与第一个节点关联起来
        final Node<E> f = first;
        final Node<E> newNode = new Node<>(null, e, f);  创建的新节点后向指针指向第一个节点
        first = newNode;
        if (f == null)
            last = newNode;
        else
            f.prev = newNode; 将第一个节点对的前向指针指向新创建的节点
        size++;
        modCount++;
    }

    /**
     * Links e as last element.
     */
    void linkLast(E e) {  创建一个新的节点,与最后一个节点关联起来
        final Node<E> l = last;
        final Node<E> newNode = new Node<>(l, e, null); 将新创建的节点指向最后的节点
        last = newNode;
        if (l == null)
            first = newNode;
        else
            l.next = newNode; 将那个最后的节点指向新创建的节点
        size++;
        modCount++;
    }
    public int indexOf(Object o) { 返回元素o在链表中的索引 若是不存在则返回-1
        int index = 0;
        if (o == null) { 
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null) 若是需要查找的元素为null,就遍历链表中的值是否有null
                    return index;
                index++;
            }
        } else {
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item)) 查找元素的索引
                    return index;
                index++;
            }
        }
        return -1;
    }
    public void clear() {  对链表进行清除
        // Clearing all of the links between nodes is "unnecessary", but:
        // - helps a generational GC if the discarded nodes inhabit
        //   more than one generation
        // - is sure to free memory even if there is a reachable Iterator
        for (Node<E> x = first; x != null; ) {
            Node<E> next = x.next;
            x.item = null; 置为null GC会对内存进行回收
            x.next = null;
            x.prev = null;
            x = next;
        }
        first = last = null;
        size = 0;
        modCount++;
    }

 

 

 

 

 

          

 

posted @ 2019-01-19 16:50  弱水三千12138  阅读(171)  评论(0编辑  收藏  举报