8.8.2 查找 替换 操作
Collections还提供了如下常用的用于查找、替换集合元素的类方法。
方法 描述
int binarySearch(List list,Object key) 使用二分搜索法搜索指定的List集合,以获得指定对象在List集合中的索引。如果要使该方法可以正常工作,则必须保证List中的元素已经处于有序状态。
| 方法 | 描述 |
|---|---|
Object max(Collection c) |
根据元素的自然顺序(基于自然排序),返回给定集合中的最大元素。 |
Object max(Collection collection, Comparator comparator) |
根据第二个Comparator参数指定的顺序,返回给定集合中的最大元素。 |
Object min(Collection c) |
根据元素的自然顺序,返回给定集合中的最小元素。 |
Object min( Collection collection, Comparator comparator) |
根据第二个Comparator参数指定的顺序,返回给定集合中的最小元素 |
void fill(List list, Object object) |
使用指定元素object替换指定List集合中的所有元素。 |
int frequency(Collection collection, Object object) |
返回指定集合中指定元素的出现次数。 |
int indexofSubList(List source, List target) |
返回target List对象在source List对象中第一次出现的位置索引;如果source List中没有出现这样的子List,则返回-1。 |
int lastIndexOfSubList(List source, List target) |
返回target List对象在source List对象中最后一次出现的位置索引;如果source List中没有出现这样的子List,则返回-1 |
boolean replaceAll(List list, Object oldValue, Object newValue) |
使用一个新值newValue替换List对象的所有旧值oldValue. |
实例
下面程序简单示范了Collections工具类的用法。
1 | import java.util.*; |
原文链接: 8.8.2 查找 替换 操作