操作集合的工具类 Collections
Java 提供了一个操作 SetList 和 Map 等集合的工具类: Collections, 该工具类里提供了大量方法对集合元素进行排序查询和修改等操作, 还提供了将集合对象设置为不可变对集合对象实现同步控制等方法
排序操作
Collections 提供了如下几个方法对 List 集合元素进行排序:
- static void reverse(List list); // 反转指定 List 集合元素的顺序
- static void shuffle(List list); // 对 list 集合元素进行随机排序(shuffle 方法模拟了 "洗牌动作")
- static void sort(List list); // 根据元素的自然顺序对指定 List 集合的元素按升序进行排序
- static void sort(List list, Comparator c); // 根据指定 Comparator 产生的顺序对 List 集合的元素进行排序
- static void swap(List list, int i, int j); // 将指定 list 集合中 i 处元素和 j 处元素进行交换
- public class Test {
- public static void main(String[] args) {
- List < Integer > list = new ArrayList < Integer > ();
- list.add(2);
- list.add(1);
- list.add(4);
- list.add(9);
- list.add(13);
- list.add(11);
- list.add(12);
- // 打印结果[2, 1, 4, 9, 13, 11, 12]
- System.out.println(list);
- //1.1 反转指定 List 集合元素的顺序
- Collections.reverse(list);
- // 打印结果[12, 11, 13, 9, 4, 1, 2]
- System.out.println(list);
- //1.2 对 list 集合进行随机排序
- Collections.shuffle(list);
- // 打印结果随机
- System.out.println(list);
- //1.3 对 list 集合进行自然排序(从小到大)
- Collections.sort(list);
- // 打印结果为[1, 2, 4, 9, 11, 12, 13]
- System.out.println(list);
- //1.4 对 list 集合进行自定义排序(这里做的倒序)
- Collections.sort(list, new Comparator < Integer > () {@Override public int compare(Integer o1, Integer o2) {
- if (o1 > o2) {
- return - 1;
- }
- if (o1 < 02) {
- return 1;
- }
- return 0;
- }
- });
- // 打印结果为[13, 12, 11, 9, 4, 2, 1]
- System.out.println(list);
- //1.5 将下标为 0 和下标为 3 的元素位置交换
- Collections.swap(list, 0, 3);
- // 打印结果为[9, 12, 11, 13, 4, 2, 1]
- System.out.println(list);
- }
- }
查找替换操作
Collections 还提供了如下用于查找替换集合元素的常用方法:
- static int binarySearch(List list, Object key); // 使用二分搜索法搜索指定 List 集合, 以获得指定对象在 List 集合中的索引如果要该方法可以正常工作, 必须保证 List 中的元素已经处于有序状态
- static Object max(Collection coll); // 根据元素的自然排序, 返回给定集合中的最大元素
- static Object max(Collection coll, Comparator comp); // 根据指定 Comparator 产生的顺序, 返回给定集合的最大元素
- static Object min(Collection coll); // 根据元素的自然排序, 返回给定集合中的最小元素
- static Object min(Collection coll, Comparator comp); // 根据指定 Comparator 产生的顺序, 返回给定集合的最小元素
- static void fill(List list, Object obj); // 使用指定元素的 obj 替换指定 List 集合中所有元素
- static int frequency(Collection c, Object o); // 返回指定元素中等于指定对象的元素数量
- static int indexOfSubList(List source, List target); // 返回 List 对象在母 List 对象中第一次出现的位置索引; 如果母 List 中没有出现这样的子 list 则返回 - 1
- static int lastIndexOfSubList(List source, List target); // 返回 List 对象在母 List 对象中最后一次出现的位置索引; 如果母 List 中没有出现这样的子 list 则返回 - 1
- static boolean replaceAll(List list, Object oldVal, Object newVal); // 使用一个新值 newVal 替换 List 对象所有旧值 oldVal
- public class Test {
- public static void main(String[] args) {
- List < Integer > list = new ArrayList < Integer > ();
- list.add(2);
- list.add(1);
- list.add(4);
- list.add(9);
- list.add(13);
- list.add(11);
- list.add(12);
- //1.1 根据元素的自然排序返回集合中的最大元素
- Integer max = Collections.max(list);
- // 打印结果 13
- System.out.println(max);
- // 将集合进行自然排序
- Collections.sort(list);
- // 打印结果[1, 2, 4, 9, 11, 12, 13]
- System.out.println(list);
- //1.2 使用二分搜索法搜索指定 List 集合, 以获得指定对象在 List 集合中的索引如果要该方法可以正常工作, 必须保证 List 中的元素已经处于有序状态
- int binarySearch = Collections.binarySearch(list, 13);
- // 打印结果 6
- System.out.println(binarySearch);
- //1.3 根据指定 Comparator 产生的顺序, 返回给定集合的最大元素
- Integer max2 = Collections.max(list, new Comparator < Integer > () {@Override public int compare(Integer o1, Integer o2) {
- if (o1 > o2) return - 1;
- if (o1 < o2) return 1;
- return 0;
- }
- });
- // 打印结果 1
- System.out.println(max2);
- //1.4 使用指定元素的 111 替换指定 List 集合中所有元素
- Collections.fill(list, 111);
- // 打印结果[111, 111, 111, 111, 111, 111, 111]
- System.out.println(list);
- //1.5 返回指定元素中等于指定对象的元素数量
- int count = Collections.frequency(list, 111);
- // 打印结果 7
- System.out.println(count);
- //2.1 重声明 2 个集合做试验
- List < Integer > list2 = new ArrayList < Integer > ();
- list2.add(1);
- list2.add(2);
- list2.add(3);
- list2.add(4);
- list2.add(5);
- list2.add(6);
- List < Integer > list3 = new ArrayList < Integer > ();
- list3.add(4);
- list3.add(5);
- //2.2 返回 List 对象在母 List 对象中第一次出现的位置索引; 如果母 List 中没有出现这样的子 list 则返回 - 1
- int indexOfSubList = Collections.indexOfSubList(list2, list);
- // 打印结果 - 1
- System.out.println(indexOfSubList);
- int indexOfSubList2 = Collections.indexOfSubList(list2, list3);
- // 打印结果 3
- System.out.println(indexOfSubList2);
- //2.4 将 list3 集合中的 4 都替换成 9
- Collections.replaceAll(list3, 4, 9);
- // 打印结果[9, 5]
- System.out.println(list3);
- }
- }
同步控制
Collections 类中提供了多个 synchronizedXxx 方法, 该方法返回指定集合对象对应的同步对象, 从而可以解决多线程并发访问集合时的线程安全问题
- public class Test {
- public static void main(String[] args){
- Set<Integer> set = Collections.synchronizedSet(new HashSet<Integer>());
- List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>());
- Map<Integer, String> map = Collections.synchronizedMap(new HashMap<Integer, String>());
- }
- }
设置不可变集合
Collections 提供了如下三个方法来返回一个不可变的集合:
- emptyXxx(); // 返回一个空的不可变的集合对象, 此处的集合既可以是 List, 也可以是 Set, 还可以是 Map
- singletonXxx(); // 返回一个包含指定对象 (只有一个或一项元素) 的不可变的集合对象, 此处的集合既可以是 List, 也可以是 Set, 还可以是 Map
- unmodifiableXxx(); // 返回指定对象的不可变视图此处的集合既可以是 List, 也可以是 Set, 还可以是 Map
上面三类方法的参数是原来集合对象, 返回值是该集合的 "只读" 版本通过上面 Collections 提供三类方法, 可以生成 "只读" 的 Collection 或 Map
- public class Test {
- public static void main(String[] args){
- List<Integer> list = Collections.emptyList();
- Set<Integer> set = Collections.singleton(121);
- Map<Integer, String> tempMap = new HashMap<Integer, String>();
- tempMap.put(1, "one");
- tempMap.put(2, "two");
- tempMap.put(3, "three");
- Map<Integer, String> unMap = Collections.unmodifiableMap(tempMap);
- // 下面任意一行代码都将引发 UnsupportedOperationException 异常
- list.add(33);
- set.add(33);
- unMap.put(33,"four");
- }
- }
来源: http://www.bubuko.com/infodetail-2493788.html