package com.bjsxt.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestCollections {
public static void main(String[] args) {
List<String> col=new ArrayList<String>();
//(1)添加元素
Collections.addAll(col, "hello","world","java","sql");
System.out.println("集合中元素的个数:"+col.size());
//(2)给集合中的元素进行排序 List集合
Collections.sort(col);
System.out.println(col);
//(3)二分搜索 ,找到返回索引,找不到返回-插入点-1
System.out.println(Collections.binarySearch(col, "java"));
System.out.println(Collections.binarySearch(col, "rape"));
//(4)填充
//Collections.fill(col, "100");
//System.out.println(col);
//(5)最大和最小
System.out.println(Collections.max(col)+"\t"+Collections.min(col));
//(6)集合中的元素进行逆向
Collections.reverse(col);
System.out.println(col);
//(7)交换
Collections.swap(col, 0, 3);
System.out.println(col);
}
}
Collections工具类中实现线程安全的几个常用的方法: