- 1.Collections概述:Collections是一个操作Set,List和Map等集合的工具类
- 2.常用方法
- 排序操作:
- • reverse(List):反转 List 中元素的顺序
- • shuffle(List):对 List 集合元素进行随机排序
- • sort(List):根据元素的自然顺序对指定 List 集合元素按升序排序
- • sort(List,Comparator):根据指定的 Comparator 产生的顺序对 List 集合元素进行排序
- • swap(List,int, int):将指定 list 集合中的 i 处元素和 j 处元素进行交换
- 查找
- • Object max(Collection):根据元素的自然顺序,返回给定集合中的最大元素
- • Object max(Collection,Comparator):根据 Comparator 指定的顺序,返回给定集合中的最大元素
- • Object min(Collection):根据元素的自然顺序,返回给定集合中的最小元素
- • Object min(Collection,Comparator):根据 Comparator 指定的顺序,返回给定集合中的最小元素
- • int binarySearch(List list,T key)在 List 集合中查找某个元素的下标,但是 List 的元素必须是 T 或 T 的子类对象,而且必须是可比较大小的,即支持自然排序的。而且集合也事先必须是有序的,否则结果不确定。
- • int binarySearch(List list,T key,Comparator c)在 List 集合中查找某个元素的下标,但是List 的元素必须是 T 或 T 的子类对象,而且集合也事先必须是按照 c 比较器规则进行排序过的,否则结果不确定。
- • int frequency(Collection c,Object o):返回指定集合中指定元素的出现次数
- 复制、替换
- • void copy(List dest,List src):将 src 中的内容复制到 dest 中
- • boolean replaceAll(List list,Object oldVal,Object newVal):使用新值替换 List 对象的所有旧值• 提供了多个 unmodifiableXxx()方法,该方法返回指定 Xxx 的不可修改的视图。
- 添加
- • boolean addAll(Collection c,T... elements)将所有指定元素添加到指定 collection 中。
- 同步
- • Collections 类中提供了多个 synchronizedXxx() 方法,该方法可使将指定集合包装成线程同步的集合,从而可以解决多线程并发访问集合时的线程安全问题
- 3.面试题:区分Collection和Collections
- Collection:集合框架中的用于存储一个一个元素的接口,又分为List和Set等子接口。
- Collections:用于操作集合框架的一个工具类。此时的集合框架包括:Set,List,Map
import org.junit.Test;
import java.util.*;
public class CollectionsTest {
@Test
public void test1(){
List list = Arrays.asList(45, 43, 65, 6, 43, 2, 32, 45, 56, 34, 23);
System.out.println(list);
// Collections.reverse(list); //反转List中元素的顺序
// Collections.shuffle(list); //随机排序
// Collections.sort(list); //自然排序
Collections.sort(list, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Integer&&o2 instanceof Integer){
Integer i1=(Integer) o1; //都是Integer强转
Integer i2=(Integer) o2;
// return i1-i2;
return -(i1.intValue()-i2.intValue());//降序
}
throw new RuntimeException("类型不匹配");
}
});
System.out.println(list);
}
@Test
public void test2(){
List list = Arrays.asList(45, 43, 65, 6, 43, 2, 32, 45, 56, 34, 23);
System.out.println(list);
Object max = Collections.max(list);
System.out.println(max);
Object max1=Collections.max(list, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof Integer && o2 instanceof Integer) {
Integer i1 = (Integer) o1; //都是Integer强转
Integer i2 = (Integer) o2;
return -(i1.intValue() - i2.intValue());//降序
}
throw new RuntimeException("类型不匹配");
}
});
System.out.println(max);
System.out.println(max1);
int count = Collections.frequency(list, 45);
System.out.println("45出现了:"+count+"次");
}
@Test
public void test3(){
List src = Arrays.asList(45, 43, 65, 6, 43, 2, 32, 45, 56, 34, 23);
// ArrayList dest = new ArrayList(); 错误的写法
// Collections.copy(dest,src);
//正确的写法:
List dest = Arrays.asList(new Object[src.size()]);
Collections.copy(dest,src);
System.out.println(dest);
}
@Test
public void test4(){
//提供多个unmodifiableXxx()方法,该方法返回指定Xxx的不可修改的视图
List list1 = new ArrayList();
//list1可以写入数据
list1.add(34);
list1.add(12);
list1.add(45);
List list2 = Collections.unmodifiableList(list1);
//list2只能读不能写
// list2.add("AA"); //报错
System.out.println(list2.get(0)); //可以取出元素
}
/*同步
*/
@Test
public void test5(){
List list1=new ArrayList();
//返回的list2是线程安全的
List list2=Collections.synchronizedList(list1);
list2.add(123);
HashMap map1=new HashMap();
Map map2 = Collections.synchronizedMap(map1);
}
}