操作集合的工具类:Collections
Java提供了一个操作Set、List和Map等集合的工具类:Collections,该工具类提供了大量方法对集合进行排序、查询和修改等操作,还提供了将集合对象置为不可变、对集合对象实现同步控制等方法。
1.排序操作
方法:
static void reverse(List<?> list): 反转列表中元素的顺序。
static void shuffle(List<?> list) : 对List集合元素进行随机排序。
static void sort(List<T> list) :根据元素的自然顺序 对指定列表按升序进行排序
static <T> void sort(List<T> list, Comparator<? super T> c) : 根据指定比较器产生的顺序对指定列表进行排序。
static void swap(List<?> list, int i, int j) :在指定List的指定位置i,j处交换元素。
static void rotate(List<?> list, int distance) :当distance为正数时,将List集合的后distance个元素“整体”移到前面;当distance为负数时,将list集合的前distance个元素“整体”移到后边。该方法不会改变集合的长度。
示例:
import java.util.*; /**
* Description:
* <br/>Copyright (C), 2005-2008, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class TestSort
{
public static void main(String[] args)
{
ArrayList nums = new ArrayList();
nums.add(2);
nums.add(-5);
nums.add(3);
nums.add(0);
//输出:[2, -5, 3, 0]
System.out.println(nums);
//将List集合元素的次序反转
Collections.reverse(nums);
//输出:[0, 3, -5, 2]
System.out.println(nums);
//将List集合元素的按自然顺序排序
Collections.sort(nums);
//输出:[-5, 0, 2, 3]
System.out.println(nums);
//将List集合元素的按随机顺序排序
Collections.shuffle(nums);
//每次输出的次序不固定
System.out.println(nums);
//后两个整体移动到前边
Collections.rotate(nums,2);
System.out.println(nums);
}
}
输出结果:
[2, -5, 3, 0]
[0, 3, -5, 2]
[-5, 0, 2, 3]
[2, 3, -5, 0]
[-5, 0, 2, 3]
2.查找、替换操作:
static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key):使用二分搜索法搜索指定列表,以获得指定对象在List集合中的索引。
**此前必须保证List集合中的元素已经处于有序状态。
static Object max(Collection coll): 根据元素的自然顺序,返回给定collection 的最大元素。
static Object max(Collection coll,Comparator comp): 根据指定比较器产生的顺序,返回给定 collection 的最大元素。
static Object min(Collection coll): 根据元素的自然顺序,返回给定collection 的最小元素。
static Object min(Collection coll,Comparator comp): 根据指定比较器产生的顺序,返回给定 collection 的最小元素。
static <T> void fill(List<? super T> list, T obj) : 使用指定元素替换指定列表中的所有元素。
static int frequency(Collection<?> c, Object o) :返回指定 collection 中等于指定对象的出现次数。
static int indexOfSubList(List<?> source, List<?> target) : 返回指定源列表中第一次出现指定目标列表的起始位置;如果没有出现这样的列表,则返回 -1。
static int lastIndexOfSubList(List<?> source, List<?> target) :返回指定源列表中最后一次出现指定目标列表的起始位置;如果没有出现这样的列表,则返回 -1。
static <T> boolean replaceAll(List<T> list, T oldVal, T newVal) :使用一个新值替换List对象的所有旧值oldVal。
示例:
import java.util.*; public class TestSearch
{
public static void main(String[] args)
{
ArrayList nums = new ArrayList();
nums.add(2);
nums.add(-5);
nums.add(3);
nums.add(0);
//输出:[2, -5, 3, 0]
System.out.println(nums);
//输出最大元素,将输出3
System.out.println(Collections.max(nums));
//输出最小元素,将输出-5
System.out.println(Collections.min(nums));
//将nums中的0使用1来代替
Collections.replaceAll(nums , 0 , 1);
//输出:[2, -5, 3, 1]
System.out.println(nums);
//判断-5 在List集合中出现的次数,返回1
System.out.println(Collections.frequency(nums , -5));
//对nums集合排序
Collections.sort(nums);
//输出:[-5, 1, 2, 3]
System.out.println(nums);
//只有排序后的List集合才可用二分法查询,输出3
System.out.println(Collections.binarySearch(nums , 3));
}
}
输出结果:
[2, -5, 3, 0]
3
-5
[2, -5, 3, 1]
1
[-5, 1, 2, 3]
3
3.同步控制:
Collectons提供了多个synchronizedXxx()方法·,该方法可以将指定集合包装成线程同步的集合,从而解决多线程并发访问集合时的线程安全问题。
正如前面介绍的HashSet,TreeSet,arrayList,LinkedList,HashMap,TreeMap都是线程不安全的。Collections提供了多个静态方法可以把他们包装成线程同步的集合。
方法如下:
static <T> Collection<T> synchronizedCollection(Collection<T> c) :返回指定 collection 支持的同步(线程安全的)collection。
static <T> List<T> synchronizedList(List<T> list) :返回指定列表支持的同步(线程安全的)列表。
static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) :返回由指定映射支持的同步(线程安全的)映射。
static <T> Set<T> synchronizedSet(Set<T> s) : 返回指定 set 支持的同步(线程安全的)set。
。。。等等有好多
示例:
import java.util.*; public class TestSynchronized
{
public static void main(String[] args)
{
//下面程序创建了四个同步的集合对象
Collection c = Collections.synchronizedCollection(new ArrayList());
List list = Collections.synchronizedList(new ArrayList());
Set s = Collections.synchronizedSet(new HashSet());
Map m = Collections.synchronizedMap(new HashMap());
}
}
多个线程访问同一个集合时设置。。
4.Collections还可以设置不可变集合,提供了如下三类方法:
emptyXxx(): 返回一个空的、不可变的集合对象,此处的集合既可以是List,也可以是Set,还可以是Map。
singletonXxx(): 返回一个只包含指定对象(只有一个或一个元素)的不可变的集合对象,此处的集合可以是:List,Set,Map。
unmodifiableXxx(): 返回指定集合对象的不可变视图,此处的集合可以是:List,Set,Map。
上面三类方法的参数是原有的集合对象,返回值是该集合的”只读“版本。
示例:
import java.util.*; public class TestUnmodifiable
{
public static void main(String[] args)
{
//创建一个空的、不可改变的List对象
List<String> unmodifiableList = Collections.emptyList();
//unmodifiableList.add("java"); //添加出现异常:java.lang.UnsupportedOperationException
System.out.println(unmodifiableList);// []
//创建一个只有一个元素,且不可改变的Set对象
Set unmodifiableSet = Collections.singleton("Struts2权威指南");//[Struts2权威指南]
System.out.println(unmodifiableSet);
//创建一个普通Map对象
Map scores = new HashMap();
scores.put("语文" , 80);
scores.put("Java" , 82);
//返回普通Map对象对应的不可变版本
Map unmodifiableMap = Collections.unmodifiableMap(scores);
//下面任意一行代码都将引发UnsupportedOperationException异常
unmodifiableList.add("测试元素");
unmodifiableSet.add("测试元素");
unmodifiableMap.put("语文",90);
}
}
用Collections工具类操作集合还是很方便的,省了很多事。。。
转发请注明出处:http://www.cnblogs.com/jycboy/p/collections.html
操作集合的工具类:Collections的更多相关文章
- 操作集合的工具类Collections
1 操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操 ...
- java之操作集合的工具类--Collections
Collections是一个操作Set.List和Map等集合的工具类. Collections中提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了对集合对象设置不可变.对集合对象实现同步控 ...
- Java-集合第六篇操作集合的工具类Collections
1.Java提供了一个操作Set.List.Map等集合的工具类:Collections. 工具类中提供的方法主要针对Set.List.Map的排序.查询.修改等操作,以及将集合对象设置为不可变.对集 ...
- JAVA基础学习之 Map集合、集合框架工具类Collections,Arrays、可变参数、List和Set集合框架什么时候使用等(4)
package com.itcast.test20140113; import java.util.ArrayList; import java.util.Arrays; import java.ut ...
- java基础37 集合框架工具类Collections和数组操作工具类Arrays
一.集合框架工具类:Collections 1.1.Collections类的特点 该工具类中所有的方法都是静态的 1.2.Collections类的常用方法 binarySearch(List< ...
- Java 集合的工具类Collections的常用方法
Collections类 java.utils.Collections是集合工具类,用来对集合进行操作. Collections类的常用方法 这里介绍四个常用方法: addAll(Collection ...
- java集合框架工具类Collections,集合的操作
1 import java.util.*; public class asList { public static void main(String args[]) { // int arr[] = ...
- 集合框架工具类--Collections排序
package ToolCollectionsDemo; import java.util.ArrayList; import java.util.Collections; import java.u ...
- Java:集合工具类-Collections
Java.util.Collections 集合框架工具类Collections,其方法都是静态的,本身没有构造函数. 常见方法: static <T extends Comparable< ...
随机推荐
- 终于找到全annotation配置springMVC的方法了(事务不失效)
如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将service bean配置到xml文件中才行 这个问题是由于问答上有解决方案 引用 这个问题很经典了 在主容器中 ...
- Git 文件比较
Git 的三个作业场: 工作区(Work Tree) 项目根目录下 .git 目录以外所有区域,是编辑项目文件的地方. 缓存区(Index) 工作区文件必须先保存在缓存区,之后从缓存区保存到仓库. 仓 ...
- outlook无法创建保存附件解决
用outlook2003收发邮件时,当打开或另存附件时提示“无法创建和保存文件” 解决 开始-运行,输入“regedit”打开注册表,打开以下键值“HKEY_CURRENT_USER\software ...
- java简单统计.java文件中的有效代码行,空行,注释行
package regxdemo; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundExc ...
- 【转载】VMware下LINUX的虚拟机增加磁盘空间
转载自:http://space.itpub.net/24435147/viewspace-694200 VMware6.7安装目录下有一个命令行工具vmware-vdiskmanager.exe 程 ...
- 开放产品开发(OPD):开篇
OPD?这是什么玩意?google一下.忘记说了,最近google被封锁的厉害,那就百度一下吧.可惜,OPD找不出是什么.你今天你找不到是正常的,因为之前还没有OPD,而现在才开始有OPD这个东东.相 ...
- [New Portal]Windows Azure Virtual Machine (10) 自定义Windows Azure Virtual Machine模板
<Windows Azure Platform 系列文章目录> 通过之前的文章,我相信大家对微软Windows Azure Virtual Machine有一定的了解了. 虽然微软提供了非 ...
- python之异常处理
异常处理是高级编程语言必备的一个功能模块. 一.异常基础 在编程过程中为了增加友好性.容错性和健壮性,在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面,通俗来说就是不让用户看见大 ...
- CentOS6.5菜鸟之旅:安装输入法(小呀小企鹅)
一.前言 假如在登录系统的时候语言选择了中文,那么是系统会自带ibus的中文输入法.但由于我打算用英文版,于是就被小企鹅输入法(FCITX)折腾了两个晚上. 二.检查系统编码 在bash中输入 loc ...
- JS魔法堂:那些困扰你的DOM集合类型
一.前言 大家先看看下面的js,猜猜结果会怎样吧! 可选答案: ①. 获取id属性值为id的节点元素 ②. 抛namedItem is undefined的异常 var nodes = documen ...