• 操作集合的工具类Collections

  Java提供了一个操作Set、List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序、查询和修改等操作,还提供了将集合对象设置为不可变、对集合对象实现同步控制等方法。

  • 排序操作

  Collections提供了如下几个方法对List集合元素进行排序:  

    1. static void reverse(List list);       //反转指定List集合元素的顺序。
    2. static void shuffle(List list);        //对list集合元素进行随机排序(shuffle方法模拟了"洗牌动作")。
    3. static void sort(List list);           //根据元素的自然顺序对指定List集合的元素按升序进行排序。
    4. static void sort(List list, Comparator c);    //根据指定Comparator产生的顺序对List集合的元素进行排序。
    5. 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还提供了如下用于查找、替换集合元素的常用方法:

    1. static int binarySearch(List list, Object key);     //使用二分搜索法搜索指定List集合,以获得指定对象在List集合中的索引。如果要该方法可以正常工作,必须保证List中的元素已经处于有序状态。
    2. static Object max(Collection coll);      //根据元素的自然排序,返回给定集合中的最大元素。
    3. static Object max(Collection coll, Comparator comp);    //根据指定Comparator产生的顺序,返回给定集合的最大元素。
    4. static Object min(Collection coll);      //根据元素的自然排序,返回给定集合中的最小元素。
    5. static Object min(Collection coll, Comparator comp);    //根据指定Comparator产生的顺序,返回给定集合的最小元素。
    6. static void fill(List list, Object obj);     //使用指定元素的obj替换指定List集合中所有元素。
    7. static int frequency(Collection c, Object o);    //返回指定元素中等于指定对象的元素数量。
    8. static int indexOfSubList(List source, List target);     //返回List对象在母List对象中第一次出现的位置索引;如果母List中没有出现这样的子list则返回-1。
    9. static int lastIndexOfSubList(List source, List  target);     //返回List对象在母List对象中最后一次出现的位置索引;如果母List中没有出现这样的子list则返回-1。
    10. 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提供了如下三个方法来返回一个不可变的集合:

    1. emptyXxx();   //返回一个空的、不可变的集合对象,此处的集合既可以是List,也可以是Set,还可以是Map。
    2. singletonXxx();     //返回一个包含指定对象(只有一个或一项元素)的、不可变的集合对象,此处的集合既可以是List,也可以是Set,还可以是Map。
    3. 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");
}
}

Java中的集合Collections工具类(六)的更多相关文章

  1. 线程高级应用-心得8-java5线程并发库中同步集合Collections工具类的应用及案例分析

    1.  HashSet与HashMap的联系与区别? 区别:前者是单列后者是双列,就是hashmap有键有值,hashset只有键: 联系:HashSet的底层就是HashMap,可以参考HashSe ...

  2. Java集合——Collections工具类

    Java集合——Collections工具类 摘要:本文主要学习了Collections工具类的常用方法. 概述 Collections工具类主要用来操作集合类,比如List和Set. 常用操作 排序 ...

  3. JAVA中的集合容器操作类

    目录 JAVA中的集合容器操作类 List集合 ArrayList的操作方法说明 LinkedList Stack Set Map Queue 总结 JAVA中的集合容器操作类 Java容器类库总共分 ...

  4. Java中各种集合(字符串类)的线程安全性!!!

    Java中各种集合(字符串类)的线程安全性!!! 一.概念: 线程安全:就是当多线程访问时,采用了加锁的机制:即当一个线程访问该类的某个数据时,会对这个数据进行保护,其他线程不能对其访问,直到该线程读 ...

  5. java基础37 集合框架工具类Collections和数组操作工具类Arrays

    一.集合框架工具类:Collections 1.1.Collections类的特点 该工具类中所有的方法都是静态的 1.2.Collections类的常用方法 binarySearch(List< ...

  6. java之操作集合的工具类--Collections

    Collections是一个操作Set.List和Map等集合的工具类. Collections中提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了对集合对象设置不可变.对集合对象实现同步控 ...

  7. java 集合Collections 工具类:排序,查找替换。Set、List、Map 的of方法创建不可变集合

    Collections 工具类 Java 提供1个操作 Set List Map 等集合的工具类 Collections ,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集合 ...

  8. java中的Arrays这个工具类你真的会用吗

    Java源码系列三-工具类Arrays ​ 今天分享java的源码的第三弹,Arrays这个工具类的源码.因为近期在复习数据结构,了解到Arrays里面的排序算法和二分查找等的实现,收益匪浅,决定研读 ...

  9. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

随机推荐

  1. javaweb项目的全局监听配置

    在项目中有时候会遇到全局监听的需求,而全局性的监听该如何配置,代码如下: package com.demo.listener; import javax.servlet.ServletContextE ...

  2. Linux系列(17)之系统服务

    我们知道,在我们登陆Linux后,系统就为我们提供了很多服务,比如例行工作调度服务crond.打印服务.邮件服务等.那么这些服务是如何被启动的呢? 这个问题先放一下,接下来我们先了解一下Linux的启 ...

  3. Pots(POJ-3414)【BFS】

    题意:有两个有着固定容量的茶壶,初始时都为空,要求用FILL,POUR,DROP三种操作来准确地得到C值,输出最少次数及操作方案. 思路:比赛的时候真是脑子不好使,根本没想到是搜索,看了别人的题解用搜 ...

  4. 【Python基础】06_Python中的函数

    1.函数的定义 def 函数名(): 函数封装的代码 …… 注:函数前后应该保留两个空行 2.函数的使用 直接使用函数名()调用函数块. def say_hello(): print("He ...

  5. Scala学习十四——模式匹配和样例类

    一.本章要点 match表达式是更好的switch,不会有意外调入下一个分支 如果没有模式能够匹配,会抛出MatchError,可以用case _模式避免 模式可以包含一个随意定义的条件,称做守卫 你 ...

  6. ASP.NET Core 2.0 中读取 Request.Body 的正确姿势

    原文:ASP.NET Core 中读取 Request.Body 的正确姿势 ASP.NET Core 中的 Request.Body 虽然是一个 Stream ,但它是一个与众不同的 Stream ...

  7. C# 使用Emit实现动态AOP框架 (三)

    目  录 C# 使用Emit实现动态AOP框架 (一) C# 使用Emit实现动态AOP框架 (二) C# 使用Emit实现动态AOP框架 (三) C# 使用Emit实现动态AOP框架 进阶篇之异常处 ...

  8. 出现 HTTP 错误 500.19 错误代码 0x800700b7

    这个内容出现主要问题是在IIS上,我们一般程序开发 iis中默认的路径只是http://localhost/,相当于环境变量中已定义好了,如果自己创建的项目直接将路径定义到这,就会替换图二中的路径,然 ...

  9. 你真的了解new function(){} 和 function(){}()吗?

    只要 new 表达式之后的 constructor 返回(return)一个引用对象(数组,对象,函数等),都将覆盖new创建的匿名对象,如果返回(return)一个原始类型(无 return 时其实 ...

  10. kubernetes 实现redis-statefulset集群

    Kubernetes 通过statefulset部署redis cluster集群 部署redis集群方式的选择 Statefulset Service&depolyment 对于redis, ...