Java中的集合Collections工具类(六)
操作集合的工具类Collections
Java提供了一个操作Set、List和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");
}
}
Java中的集合Collections工具类(六)的更多相关文章
- 线程高级应用-心得8-java5线程并发库中同步集合Collections工具类的应用及案例分析
1. HashSet与HashMap的联系与区别? 区别:前者是单列后者是双列,就是hashmap有键有值,hashset只有键: 联系:HashSet的底层就是HashMap,可以参考HashSe ...
- Java集合——Collections工具类
Java集合——Collections工具类 摘要:本文主要学习了Collections工具类的常用方法. 概述 Collections工具类主要用来操作集合类,比如List和Set. 常用操作 排序 ...
- JAVA中的集合容器操作类
目录 JAVA中的集合容器操作类 List集合 ArrayList的操作方法说明 LinkedList Stack Set Map Queue 总结 JAVA中的集合容器操作类 Java容器类库总共分 ...
- Java中各种集合(字符串类)的线程安全性!!!
Java中各种集合(字符串类)的线程安全性!!! 一.概念: 线程安全:就是当多线程访问时,采用了加锁的机制:即当一个线程访问该类的某个数据时,会对这个数据进行保护,其他线程不能对其访问,直到该线程读 ...
- java基础37 集合框架工具类Collections和数组操作工具类Arrays
一.集合框架工具类:Collections 1.1.Collections类的特点 该工具类中所有的方法都是静态的 1.2.Collections类的常用方法 binarySearch(List< ...
- java之操作集合的工具类--Collections
Collections是一个操作Set.List和Map等集合的工具类. Collections中提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了对集合对象设置不可变.对集合对象实现同步控 ...
- java 集合Collections 工具类:排序,查找替换。Set、List、Map 的of方法创建不可变集合
Collections 工具类 Java 提供1个操作 Set List Map 等集合的工具类 Collections ,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集合 ...
- java中的Arrays这个工具类你真的会用吗
Java源码系列三-工具类Arrays 今天分享java的源码的第三弹,Arrays这个工具类的源码.因为近期在复习数据结构,了解到Arrays里面的排序算法和二分查找等的实现,收益匪浅,决定研读 ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
随机推荐
- python进程池 使用Queue实现计数功能
多进程中各个进程间相互隔离,进程间通信需要使用到通道. 多进程中使用Queue实现进程中通信 from multiprocessing import Process,Queue import time ...
- [转帖]Asp.net MVC 与 Asp.net Web API 区别
Asp.net MVC 与 Asp.net Web API 区别 https://www.cnblogs.com/viktor988/ https://www.cnblogs.com/terry283 ...
- vue中可以自定义动画的前缀
vue中可以自定义动画的前缀1.只需在中加入name属性即可 <transition name="my"> <h6 v-if="flag2"& ...
- mysql数据库基础-2019-9-10(随堂笔记)
mysql数据库基础 在cmd情况下启动mysql数据库:(配置path环境变量后可忽略) 运行mysql1. 进入mysql路径2. 执行:mysql -uroot -p,安装时的密码 1.数据库& ...
- scrapy-redis数据去重与分布式框架
数据去重 生成指纹:利用hashlib的sha1,对request的请求体.请求url.请求方法进行加密,返回一个40位长度的16进制的字符串,称为指纹 fp = hashlib.sha1() fp. ...
- Scala当中什么是RDD(Resilient Distributed Datasets)弹性分布式数据集
RDD(Resilient Distributed Datasets)弹性分布式数据集.你不好理解的话,可以把RDD就可以看成是一个简单的"动态数组"(比如ArrayList),对 ...
- 数据结构C++实现-第一章 绪论
1.1 计算机与算法 1.1.3 起泡排序 void bubbleSort(int a[], int n) { for(bool sorted=false; !sorted; --n) { sorte ...
- python学习-7 条件语句 while循环 + 练习题
1.死循环 while 1 == 1: print('ok') 结果是一直循环 2.循环 count = 0 while count < 10: print(count) count = cou ...
- while循环,格式化输出,运算符及编码初识
一.while循环 1.基本循环(死循环) while 条件: 循环体 2.使用while计数 count = 0 # 数字里面非零的都为True while True: count = count ...
- 13.lsof恢复删除的文件
[root@temp ~]# lsof -p 5643COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEoracle 5643 ...