美文网首页Java基础
集合操作类:Collections

集合操作类:Collections

作者: 黄金矿工00七 | 来源:发表于2018-07-20 18:21 被阅读24次

    关于Collections的描述上一篇文章已经说过,这里我们主要来看一下Collections的实现以及他所提供的静态方法。

    //使用private修饰构造函数,确保单例模式
    private Collections() {
        }
    
    • 集合排序、二分查找、列表逆置


      图片.png
    • List初始化,复制,求极值


      图片.png
    • 获取线程安全集合:


      图片.png

      我说一下大概的实现原理,以List为例,首先判断List是否实现Randomaccess接口(主要目的是使算法能够在随机和顺序访问的list中表现的更加高效。),关于这个接口可自行百度,不是这里的重点,然后生成相应的SynchronizedList或者SynchronizedRandomAccessList(两者只是算法实现不同,实现原理是一样的),我们看一下源码:

     static class SynchronizedList<E>
            extends SynchronizedCollection<E>
            implements List<E> {
            private static final long serialVersionUID = -7754090372962971524L;
    
            final List<E> list;
    
            SynchronizedList(List<E> list) {
                super(list);
                this.list = list;
            }
            SynchronizedList(List<E> list, Object mutex) {
                super(list, mutex);
                this.list = list;
            }
    
            public boolean equals(Object o) {
                if (this == o)
                    return true;
                synchronized (mutex) {return list.equals(o);}
            }
            public int hashCode() {
                synchronized (mutex) {return list.hashCode();}
            }
    
            public E get(int index) {
                synchronized (mutex) {return list.get(index);}
            }
            public E set(int index, E element) {
                synchronized (mutex) {return list.set(index, element);}
            }
            public void add(int index, E element) {
                synchronized (mutex) {list.add(index, element);}
            }
            public E remove(int index) {
                synchronized (mutex) {return list.remove(index);}
            }
    
            public int indexOf(Object o) {
                synchronized (mutex) {return list.indexOf(o);}
            }
            public int lastIndexOf(Object o) {
                synchronized (mutex) {return list.lastIndexOf(o);}
            }
    
            public boolean addAll(int index, Collection<? extends E> c) {
                synchronized (mutex) {return list.addAll(index, c);}
            }
    
            public ListIterator<E> listIterator() {
                return list.listIterator(); // Must be manually synched by user
            }
    
            public ListIterator<E> listIterator(int index) {
                return list.listIterator(index); // Must be manually synched by user
            }
    
            public List<E> subList(int fromIndex, int toIndex) {
                synchronized (mutex) {
                    return new SynchronizedList<>(list.subList(fromIndex, toIndex),
                                                mutex);
                }
            }
    
            @Override
            public void replaceAll(UnaryOperator<E> operator) {
                synchronized (mutex) {list.replaceAll(operator);}
            }
            @Override
            public void sort(Comparator<? super E> c) {
                synchronized (mutex) {list.sort(c);}
            }
    
        
            private Object readResolve() {
                return (list instanceof RandomAccess
                        ? new SynchronizedRandomAccessList<>(list)
                        : this);
            }
        }
    

    实际上你会发现,SynchronizedList的实现只是内部维护了我们传入的list集合对象,然后对大部分方法用synchronized进行修饰,锁可以根据需要自己定义或者使用list本身,没有什么高深的原理

    • 返回视图


      图片.png

      视图我们知道是不可修改的,只能进行查询操作
      下面我们来看一下是如何实现生成视图的操作的:以List为例

     static class UnmodifiableList<E> extends UnmodifiableCollection<E>
                                      implements List<E> {
            private static final long serialVersionUID = -283967356065247728L;
    
            final List<? extends E> list;
    
            UnmodifiableList(List<? extends E> list) {
                super(list);
                this.list = list;
            }
    
            public boolean equals(Object o) {return o == this || list.equals(o);}
            public int hashCode()           {return list.hashCode();}
    
            public E get(int index) {return list.get(index);}
            public E set(int index, E element) {
                throw new UnsupportedOperationException();
            }
            public void add(int index, E element) {
                throw new UnsupportedOperationException();
            }
            public E remove(int index) {
                throw new UnsupportedOperationException();
            }
            public int indexOf(Object o)            {return list.indexOf(o);}
            public int lastIndexOf(Object o)        {return list.lastIndexOf(o);}
            public boolean addAll(int index, Collection<? extends E> c) {
                throw new UnsupportedOperationException();
            }
    
            @Override
            public void replaceAll(UnaryOperator<E> operator) {
                throw new UnsupportedOperationException();
            }
            @Override
            public void sort(Comparator<? super E> c) {
                throw new UnsupportedOperationException();
            }
    
            public ListIterator<E> listIterator()   {return listIterator(0);}
    
            public ListIterator<E> listIterator(final int index) {
                return new ListIterator<E>() {
                    private final ListIterator<? extends E> i
                        = list.listIterator(index);
    
                    public boolean hasNext()     {return i.hasNext();}
                    public E next()              {return i.next();}
                    public boolean hasPrevious() {return i.hasPrevious();}
                    public E previous()          {return i.previous();}
                    public int nextIndex()       {return i.nextIndex();}
                    public int previousIndex()   {return i.previousIndex();}
    
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                    public void set(E e) {
                        throw new UnsupportedOperationException();
                    }
                    public void add(E e) {
                        throw new UnsupportedOperationException();
                    }
    
                    @Override
                    public void forEachRemaining(Consumer<? super E> action) {
                        i.forEachRemaining(action);
                    }
                };
            }
    
            public List<E> subList(int fromIndex, int toIndex) {
                return new UnmodifiableList<>(list.subList(fromIndex, toIndex));
            }
    
     \
            private Object readResolve() {
                return (list instanceof RandomAccess
                        ? new UnmodifiableRandomAccessList<>(list)
                        : this);
            }
        }
    
    

    同样进行上面的一个是否实现RandomAccess接口的操作,不在赘述,直接看UnmodifiableList的源码,同样的内部维护了我们传入的list集合对象,对所有update操作抛出异常,同样无法获取ListIterator,就无法进行修改操作

    相关文章

      网友评论

        本文标题:集合操作类:Collections

        本文链接:https://www.haomeiwen.com/subject/cwvqmftx.html