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工具类的更多相关文章
- Java集合框架:Collections工具类
java.util.Collections工具类提供非常多实用的方法.使得程序员操作集合类的时候更加的方便easy,这些方法都是静态的. 整个Collections工具类源代码几乎相同有4000行.我 ...
- Java集合(1):Collections工具类中的static方法
与Arrays一样,Collections类中也有一些实用的static方法. (1) 排序操作 reverse(List list):反转指定List集合中元素的顺序 shuffle(List li ...
- Java集合框架:Arrays工具类
java.util.Arrays类能方便地操作数组,它提供的方法都是静态的.整个Arrays工具类的实现有3000+行.可是归纳总结一下可知它有下面功能(9个): 1. asList 定义: @Saf ...
- Java集合框架GS Collections具体解释
Java集合框架GS Collections具体解释 作者:chszs.未经博主同意不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs GS Collec ...
- Java可变参数与Collections工具类使用了解
今天发现jdk1.5后增加了个可变参数,以前还一直不晓得 public static void main(String[] args) { System.out.println(getNum(1,2, ...
- 双列集合Map接口 & Collections工具类
HashMap 常用方法 遍历方式 iterator迭代器 ITIT HashTable 继承字典 Hashtable--Properties 文件读写 总结 Collections工具类
- Java 集合系列之六:工具类Collections和Arrays基本操作
1. Collections Collections类主要是完成了两个主要功能 提供了若干简单而又有用的算法,比如排序,二分查找,求最大最小值等等. 提供对集合进行包装的静态方法.比如把指定的集合包装 ...
- Java集合框架的接口和类层次关系结构图
Collection和Collections的区别 首先要说的是,"Collection" 和 "Collections"是两个不同的概念: 如下图所示,&qu ...
- java集合框架之Collections
参考http://how2j.cn/k/collection/collection-collections/369.html Collections是一个类,容器的工具类,就如同Arrays是数组的工 ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
随机推荐
- Linxu指令--date,cal
在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数 ...
- openvpn服务端与客户端网段互通
http://www.softown.cn/post/140.html OpenVPN安装.配置教程 http://www.softown.cn/post/137.html openvpn的serve ...
- 查看Eclipse运行工程时使用的Command Line
一.查看使用的Command Line 1.Window -> Show View -> Other... -> Debug 2.运行工程,然后在Debug视窗中找到运行的工程的主线 ...
- xBIM 应用与学习 (二)
目录 xBIM 应用与学习 (一) xBIM 应用与学习 (二) xBIM 基本的模型操作 xBIM 日志操作 XBIM 3D 墙壁案例 xBIM 格式之间转换 xBIM 使用Linq 来优化查询 x ...
- CentOS7安装WDCP3
CentOS7安装WDCP3.2面板教程 到此WDCP安装完毕
- java签名与验签
基本概念: 加密解密 加密:发送方利用接收方的公钥对要发送的明文进行加密. 解密:接收方利用自己的私钥进行解密. 公钥和私钥配对的,用公钥加密的文件,只有对应的私钥才能解密.当然也可以反过来,用私钥加 ...
- 洛谷 [P3258] 松鼠的新家
树上差分 对于一条路径 \(u->v\) 来说,设 \(t=LCA(u,v)\) ,d[]为差分数组 ,则有 d[u]++;d[v]++;d[t]--;d[fa[t]]--; 注意:题目中所给的 ...
- 如何用JavaScript制作循环图形
[图题思路] 1.确定图形一共几行,即为外层的循环的次数 2.确定每行有几种元素,代表有几个内层循环 3.确定每种元素的个数,即为每个内层循环的次数Tips:通常,找出每种元 ...
- ssh: Could not resolve hostname git.*****-inc.com : Temporary failure in name resolution fatal: The remote end hung up unexpectedly
问题出现的情景:使用git pull拉取开发的代码到测试服务器,报错: ssh: Could not resolve hostname git.****-inc.com : Temporary fai ...
- SaltStack 的远程执行机制
html,body { font-size: 15px } body { font-family: Helvetica, "Hiragino Sans GB", "微软雅 ...