| Collections工具类Collections是一个操作Set,List和Map等集合的工具类Collections中提供了一系列静态的方法对集合元素进行排序,查询和修改等操作,还提供了对集合对象设置不可变,对几个对象实现同步控制等方法排序操作:(均为static方法)
  
 reverse(List): 反转List中元素的顺序shuffle(List):对List集合元素进行随机排序
 sort(List,Comparator): 根据指定的Comparator产生的顺序对List集合元素进行排序
 swap(List,int ,int ): 将指定list集合中的i处元素和j处元素进行交换
 Collections常用方法 >Collections.copy(List dest,List src); //使用时需要注意目标集合的长度要大于等于源集合长度
     @Test
    public void test01(){
         List list = new ArrayList();
        list.add(456);
        list.add(45);
        list.add(6);
        list.add(-6);
        List dest = Arrays.asList(new Object[list.size()]); 
        Collections.copy(dest,list);
        System.out.println(dest);
    }
}
 Collections类中提供了多个synchronizedXxx()方法该方法可以使将指定集合包装成线程同步的集合,从而可以解决多线程并发访问集合时的线程安全问题
  ArrayList和HashMap都是 线程不安全的,如果我们需要线程安全的,我们也不因此就选择Vector和Hashtable线程安全的,而是想办法把ArrayList和HashMap变成线程安全的 Collections.synchronizedList()方法把List集合变成线程安全的List集合 
 //返回的list1即为线程安全的ListList list1 = Collections.synchronizedList(list);
 底层部分源码:  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);}
        }
 面试题:Collection和Collections的区别?
 Collection是创建集合的一个接口,存储单列数据的集合接口(List,Set接口)
 Collections是一个工具类,操作Collection,Map的工具类
 |