集合操作类:Collections

关于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,就无法进行修改操作

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,023评论 3 119
  • 一、联网获取所需下载文件的长度 二、构建缓存文件(遍历缓存目录里的文件判断文件是否可以断点续传) 判该文件是否可以...
    Sunsol阅读 1,056评论 1 2
  • 随着互联网时代的来临,我们体会到了前所未有的便利,如今互联网对我们的影响已经根深蒂固并渗透到生活中的各个方面,我们...
    qishuangyan阅读 852评论 0 0
  • 这篇文章是我自己学习runtime时候的笔记,希望能有助于大家了解runtime,水平有限有需要补充或者指...
    努力心安阅读 257评论 0 0
  • 外面呜呜的风声搅得我心烦意乱,便起身把宿舍的窗子关紧一点。车子怎么还没来啊,我等得有点焦躁,我想家了。今年是我读...
    讲故事的人天秤阅读 300评论 0 0