Collections和Arrays
Collections可以创建、操作、和包装集合(包装为线程安全,或不可修改)
Arrays可以操作数组,把数组转换成List接口的类型
notes: 所有代码均为<<Java 技术手册(第6版)>>一书摘录
Collections
// 为包装的集合提供线程安全性
// 通过包装集合修改集合后,改动也会体现在原集合身上;反之亦然
List<String> list = Collections.synchronizedList(new ArrayList<>());
Set<Integer> set = Collections.synchronizedSet(new HashSet<>());
Map<String, Integer> map = Collections.synchronizedMap(new HashMap<>());
// 包装方法创建的集合对象不能修改底层集合
List<Integer> primes = new ArrayList<Integer>();
List<Integer> readonly = Collections.unmodifiableList(primes);
// 可以修改primes列表
primes.addAll(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19));
// 但不能修改只读的包装集合
// readonly.add(23); // 抛出UnsupportedOperationException异常
Collections.sort(list);
// 必须先排序列表中的元素
int pos = Collections.binarySearch(list, "key");
// 把list2中的元素复制到list1中,覆盖list1
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
Collections.copy(list1, list2);
// 使用对象o填充list
Collections.fill(list, "1");
// 分别返回不可变的 Set、 List 和 Map 对象
Set<Integer> singleton = Collections.singleton(1);
Collection<Integer> c = Collections.singletonList(1);
Map<String, Integer> readyOnly = Collections.singletonMap("666", 1);
// 找出集合c中最大的元素
Collections.max(c);
// 找出集合c中最小的元素
Collections.min(c);
Collections.reverse(list); // 反转列表
Collections.shuffle(list); // 打乱列表
/**
* 如果你编写的方法要返回一个集合,遇到没有返回值的情况时,
* 一般最好返回空集合,而不要返回 null 等特殊的值
*/
// 返回空集合的方法
Set<Integer> si = Collections.emptySet();
List<String> ss = Collections.emptyList();
Map<String, Integer> m = Collections.emptyMap();
// 返回一个不可变的 List 对象,包含指定数量个指定对象的副本
List<Map.Entry<Integer, String>> l = Collections.nCopies(100, Map.entry(0, "女"));
List<Integer> tenzeros = Collections.nCopies(10, 0);
Arrays
String[] a = {"this", "is", "a", "test"}; // 一个数组
// 把数组当成大小不可变的列表
List<String> l = Arrays.asList(a);
// 创建一个大小可变的副本
List<String> m = new ArrayList<String>(l);
// asList()是个变长参数方法,所以也可以这么做:
Set<Character> abc = new HashSet<Character>(Arrays.asList('a', 'b', 'c'));
Set<Integer> set = new HashSet<>();
List<String> list = new ArrayList<>();
Map<String, Integer> map = new HashMap<>();
// Collection接口定义了toArray()方法。不传入参数时,这个方法创建
// Object[]类型的数组,把集合中的元素复制到数组中,然后返回这个数组
// 把set中的元素存入数组
Object[] members = set.toArray();
// 把list中的元素存入数组
Object[] items = list.toArray();
// Map的三种数据访问视图都可以转成数组
// 把map的键存入数组
Object[] keys = map.keySet().toArray();
// 把map的值存入数组
Object[] values = map.values().toArray();
// 把map的键值对存入数组
Object[] pairs = map.entrySet().toArray();
// 如果不想返回Object[]类型的值,可以把一个所需类型的数组传入toArray()方法
// 如果传入的数组不够大,会再创建一个相同类型的数组
// 如果传入的数组太大,复制集合元素后剩余的位置使用null填充
String[] arr1 = l.toArray(new String[0]);
String[] arr2 = l.toArray(String[]::new);
String[] array = l.toArray(new String[l.size()-1]);
int[] intarray = new int[] { 10, 5, 7, -3 }; // 由整数组成的数组
Arrays.sort(intarray); // 原地排序数组
// 在索引2找到值7
pos = Arrays.binarySearch(intarray, 7);
// 未找到:返回负数
pos = Arrays.binarySearch(intarray, 12);
// 由对象组成的数组也能排序和搜索
String[] strarray = new String[] { "now", "is", "the", "time" };
Arrays.sort(strarray); // 排序的结果:{ "is", "now", "the", "time" }
// Arrays.equals()方法比较两个数组中的所有元素
String[] clone = (String[]) strarray.clone();
boolean b1 = Arrays.equals(strarray, clone); // 是的,两个数组相等
// Arrays.fill()方法用于初始化数组的元素
// 一个空数组,所有元素都是0
byte[] data = new byte[100];
// 把元素都设为-1
Arrays.fill(data, (byte) -1);
// 把第5-9个元素设为-2
Arrays.fill(data, 5, 10, (byte) -2);
数组相关的其它方法
字符串转字符数组,这个方法很实用
Class对象可以知道它自己是不是一个数组,如果是数组,getComponentType
可以获取数组元素的类型
char[] text = "Now is the time".toCharArray();
char[] copy = new char[100];
// 从text的第4个元素开始,复制10个字符到copy中
// 这10个字符的位置从copy[0]开始
System.arraycopy(text, 4, copy, 0, 10);
// 把一些元素向后移,留出位置插入其他元素
System.arraycopy(copy, 3, copy, 6, 7);
int[] o = {1, 2, 3, };
// The Class object that represents the runtime class of this object.
Class type = o.getClass();
// true if this object represents an array class, false otherwise.
if (type.isArray()) {
// Returns the Class representing the component type of an array.
// If this class does not represent an array class this method returns null.
Class elementType = type.getComponentType();
}