JavaSE-集合-Collections工具类常用方法

在这里插入图片描述
在这里插入图片描述

1、reverse(List):反转List中元素的顺序

在这里插入图片描述

2、shuffle(List):对List集合元素进行随机排序

在这里插入图片描述

3、sort(List):根据元素的自然顺序对指定的List集合元素按升序排序

在这里插入图片描述

4、sort(List,Comparator):根据指定的Comparator产生的顺序对List集合元素进行排序

5、swap(List,int,int):将指定List集合中的i处元素和j处元素进行交换

在这里插入图片描述

6、Object max(Collection):根据元素的自然顺序,返回给定集合中的最大元素

7、Object max(Collection,Comparator):根据Comparator指定的顺序,返回给定集合中最大的元素

8、Object min(Collection)

9、Object min(Collection,Comparator)

10、int frequency(Collection,Object):返回指定集合中指定元素出现的次数

在这里插入图片描述

11、void copy(List dest,List src):将src中的内容赋值到List中

在这里插入图片描述

12、boolean replaceAll(List list,Object oldVal,Object newVal):使用新值替换原来的List集合所有旧值

13、Collections类中的Synchronized()方法

Collections类中提供了多个Synchronized()方法,
该方法可使指定集合包装成线程同步的集合,
从而可以解决多线程并发访问集合时的线程安全 。

//返回的list1集合是线程安全的
@Test
public void test03(){
    
    
    List list = new ArrayList();
    list.add(123);
    list.add(456);
    List list1 = Collections.synchronizedList(list);
}

猜你喜欢

转载自blog.csdn.net/qq_46112274/article/details/124721501