19、常用类——Collections 类

Collections 类

Collections 则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素进行排序、搜索以及线程安全等各种操作

常用方法

public static <T extends Comparable<? super T>> void sort(List list)
根据元素的自然顺序对指定列表按升序进行排序
参数:list - 要排序的列表

public static void shuffle(List<?> list)
随机排序
参数:list - 要改组的列表

public static int binarySearch(List<? extends Comparable<? super T>> list,T key)
使用二分搜索法搜索指定列表,以获得指定对象
参数:list - 要搜索的列表、key - 要搜索的值

public static void reverse(List<?> list)
反转指定列表中元素的顺序
参数:list - 元素要被反转的列表

public static boolean addAll(Collection<? super T> c,T… elements)
将所有指定元素添加到指定 collection 中
参数:c - 要插入 elements 的 collection,elements - 插入 c 的元素
例如:

ArrayList<String> a1 = new ArrayList<String>();
ArrayList<String> a2 = new ArrayList<String>();

a1.add("学");
a1.add("好");
a1.add("Java");
	    
a2.add("做");
a2.add("好");
a2.add("程序");

Collections.copy(a2, a1);
System.out.println(a2);

public static T max(Collection<? extends T> coll)
public static T min(Collection<? extends T> coll)
求最大最小数
参数:coll - 将确定其最小元素的 collection

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值