操作集合的工具类: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< ...
随机推荐
- 简单Bat文件编写
@ECHO OFFTITLE 清理Work目录E:cd E:\Android_WorkSpace@ECHO ONecho 开始打包MavenTest......mvn install pause @E ...
- common.css
/* global */ document,body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,p,blockqu ...
- 超实用的JavaScript技巧及最佳实践(下)
1.使用逻辑符号&&或者||进行条件判断 1 2 3 var foo = 10; foo == 10 && doSomething(); // is the same ...
- 扩展 IEnumerable<T>,让它根据另一个集合的顺序来排列
假如我有两个集合: public class Teacher { public int Id { get; set; } public string Name { get; set; } } publ ...
- Intel HAXM安装错误处理:(TV-x) is not turned on
Android x86模拟器Intel Atom x86 System Image时提示Intel execute disable bit(xd) is not turned on 运行Elicpse ...
- MyEclipse8.5集成Tomcat7时的启动错误:Exception in thread “main” java.lang.NoClassDefFoundError org/apache/commons/logging/LogFactory
今天,安装Tomcat7.0.21后,单独用D:\apache-tomcat-7.0.21\bin\startup.bat启动web服务正常.但在MyEclipse8.5中集成配置Tomcat7后,在 ...
- 为什么移动Web应用程序很慢(译)
前些日子,看到Herb Sutter在自己的博客中推荐了一篇文章<Why mobile web apps are slow>,在推荐里他这样写道: “I don’t often link ...
- CentOS6.5菜鸟之旅:安装ATI显卡驱动
一.前言 自从安装了CentOS,我的显卡就没消停过,一直在彪高温而且噪音特别大,于是决定上网搜索解决办法.下面记录下来以供日后查阅. 二.安装fglrx driver(ATI/AMD 显卡的linu ...
- 连接不上mysqlworkbench问题解决方法
连接mysqlworkbench出现如下提示: 查看ip 加入host的范围 mysql> select user,host from mysql.user;+--------- ...
- Entity FrameWork 延迟加载本质(二)
1.对于外键实体而言,EF会在用到这个外键属性的时候,才会去查对应的表.这就是按需加载了... 2.按需加载的缺点:每次调用外键实体的时候,都会去查询数据库(EF有小优化:相同的外键实体只查一次) I ...