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 ...
随机推荐
- 改变checkbox默认样式
input[type='checkbox']{ width: 5rem; height: 5rem; -webkit-appearance: none; /*清除复选框默认样式*/ backgroun ...
- HTTP请求方法:GET、HEAD、POST、PUT、DELETE、CONNECT、OPTIONS、TRACE 说明
平时的Rest开发,用到的都是GET,POST,PUT,DELETE类型的请求. 但Rest支持的请求类型不止前面4种,还有其他几种. 下面部分转自: https://www.html.cn/arch ...
- oracle-function-into时为null报错
oracle-function-into时为null报错 create or replace function P_ADD_CUSTOMER_FOR_CSS_heyt_test(i_cust_name ...
- python+selenium+chrome实现自动登录百度
#python3.4+selenium3.5+chrome版本 63.0.3239.132+chrome驱动chromedriver.exe #实现自动登录百度 from selenium impor ...
- 怎样理解ECMAScript 和 JavaScript的关系
JavaScript可以分为三大部分: 1. 核心语法 2. DOM 3. BOM 而核心语法实际上就是指的ECMAScript, 而JS又是不断在发展的, 而这个发展实际上最主要的就是ECMAScr ...
- oracle基本查询01
/*数据库-----> 数据库实例----->表空间[逻辑单位]------>数据文件[物理单位] 通常情况下oracle数据库只会有一个实例ORCL 新建一个项目: MYSQL:创 ...
- c#OpenCVSharp+Zxing识别条形码
参考博客:https://www.cnblogs.com/dengxiaojun/p/5278679.html,但是他的demo下载太贵了 可以下载这个https://download.csdn.ne ...
- springboot-异步、发送邮件(一)
pom.xml <!--邮件javax.mail--> <dependency> <groupId>org.springframework.boot</gro ...
- iOS UIControl 事件的说明(转)
在控件事件中,简单解释下下面几个事件. 说明:由于是在“iOS 模拟器”中测试的,所以不能用手指,只能用鼠标. 1)UIControlEventTouchDown 指鼠标左键按下(注:只是“按下”)的 ...
- arcgis js之点击获取featureLayer中的点
arcgis js之点击获取featureLayer中的点 代码: this.view.on('click', (evt) => { let layer = this.map.findLayer ...