操作集合的工具类:Collections使用示例
来源:互联网 发布:mac重启黑屏 编辑:程序博客网 时间:2024/05/23 20:54
操作集合的工具类:Collections
提供包括元素的排序、查询、修改等操作,还实现将集合对象设置为不可变类,对集合对象实现同步控制等
使用实例1:普通操作
imp
imp
public class TestCol {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList al = new ArrayList();
al.add(2);
al.add(-5);
al.add(3);
al.add(0);
System.out.println(al);
//输出最大、最小值
System.out.println(Collections.max(al));
System.out.println(Collections.min(al));
//替换
Collections.replaceAll(al, 0, 1);
System.out.println(al);
//判断-5在集合中出现的次数
System.out.println(Collections.frequency(al, -5));
//排序
Collections.sort(al);
System.out.println(al);
//二分查找
System.out.println(Collections.binarySearch(al, -5));
}
}
实例2:同步控制
public class TestSynchronized {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Collection c = Collections.synchronizedCollection(new ArrayList());
List l = Collections.synchronizedList(new ArrayList());
Set s = Collections.synchronizedSet(new HashSet());
Map m = Collections.synchronizedMap(new HashMap());
}
}
实例3:设置不可变类
public class TestUnmodifiable {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List ul = Collections.emptyList();
Set us = Collections.singleton("this is a Set");
Map m = new HashMap();
m.put("语文", 80);
m.put("数学", 60);
Map um = Collections.unmodifiableMap(m);
//以下代码引发异常
ul.add("hello");
us.add("hello");
um.put("语文", 90);
}
}
- 操作集合的工具类:Collections使用示例
- Collections 操作集合的工具类
- 操作集合的工具类 collections
- 操作集合的工具类:Collections
- 操作集合的工具类:Collections
- Java 操作集合的工具类:Collections
- 操作集合的工具类:Collections
- 操作集合的工具类:collections
- 操作集合的工具类:Collections
- 操作集合的工具类:Collections
- Collections集合操作的工具类
- 操作集合的工具类Collections
- 操作集合的工具类Collections
- 操作集合的工具类Collections
- 1006--Java集合--操作集合的工具类:Collections
- 类 Collections集合的操作
- Collections(集合操作的工具类)&集合的同步控制&不可变集合的设置
- Collections(集合操作的工具类)&集合的同步控制&不可变集合的设置
- linux下删除文件夹
- PHP简单语法的详细介绍
- Android中Sqlite的时间函数(翻译)
- 常用Linux打包命令 tar和zip
- HDU 1358(kmp)
- 操作集合的工具类:Collections使用示例
- Ubuntu 11.10 Android编译环境安装
- appStore上传苹果应用程序软件发布流程
- PHP脚本代码标记
- 辽宁移动梦网短信新增业务申报流程
- Struts Ongl valueStack与ActionContent.ValueStack的区别
- 360浏览器插件360抢票王火车票刷票技巧
- 黑马程序员:多线程
- PHP的输出