• 操作集合的工具类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. (十三)mybatis 整合 ehcache

    目录 ehcache mybatis 的 Cache 接口 整合步骤 ehcache ehcache 是一个分布式缓存框架 ! 为什么需要分布式缓存? 在大型的项目中,服务器是肯定不止一台的,每台服务 ...

  2. linux命令自动补全

    在linux中命令较长时,不易记忆,使用命令行自动补全,使用方便,配置方法记录如下 需要安装bash-completion 重启后生效 命令:yum install bash-completion,安 ...

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

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

  4. Oracle设置权限和还原数据库

    Oracle还原数据库 ,在最高权限账户上,先将安装好的数据上创建一个账户 -- Create the user create user newsafe identified by newsafe d ...

  5. Web API与MVC控制器的区别

    Web API属于ASP.NET核心平台的一部分,它利用MVC框架的底层功能方便我们快速的开发部署WEB服务.我们可以在常规MVC应用通过添加API控制器来创建web api服务,普通MVC应用程序控 ...

  6. Java Web-servlet、HTTP in servlet和捎带的Java绘图学习

    Java Web-servlet.HTTP in servlet和捎带的Java绘图学习 server applet:运行在服务器端的小程序 动态项目的动态内容的java类依赖于服务器才能运行,由to ...

  7. seaborn:一个更强大的画图工具

    数据加载: 1. Countplot 和之前的pandas绘图相比,使用countplot可以自动计算每类的数量. 2. KDE Plot KDE,是"kernel density esti ...

  8. 一些常见css样式加选择器

    先来个下拉框: 例: select是下拉框标签 multiple:指多选项 size :显示的选项数量 selected="selected":默认选中 —————————分割线— ...

  9. vue生命周期详细过程

  10. 一种无法被Dump的jar包加密保护解决方案

    作者: 我是小三 博客: http://www.cnblogs.com/2014asm/ 由于时间和水平有限,本文会存在诸多不足,希望得到您的及时反馈与指正,多谢! 工具环境: windwos10.I ...