Java Collections工具类
2015-07-13 10:24
447 查看
Collections作为Collection接口的工具类,为Collection接口对象的使用提供了大量的方法。注意Collection是接口,定义的是一组标准;而Collections是为Collection的对象提供操作方便的工具,二者不存在继承或实现的关系。此类完全由静态方法组成,下面介绍下Collections的常用方法。
// 按自然顺序对指定的对象列表按升序进行排序
static <T extends Comparable<? super T>> void sort(List<T> list);
// 按指定比较器产生的顺序对指定List按升序进行排序
static <T extends Comparable<? super T>> void sort(LIst<T> list, Comparator<? super T> c);
// 使用二分查找,获取指定对象;使用对象比较器
static <T> binarySearch(List<? extends Comparable<? super T>> list, T key);
// 使用二分查找,获取指定对象;使用指定比较器
static <T> binarySearch(List<? extends T> list, T key, Comparator<? super T> c);
// 根据元素的自然顺序,返回给定collection的最大元素
static <T extends Object & Comparable<? super T>> T max(collection<? extends T> coll);
// 根据指定比较器产生的顺序,返回给定collection的最大元素
static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp);
// 根据元素的自然顺序,返回给定collection的最小元素
static <T extends Object & Comparable<? super T>> T min(collection<? extends T> coll);
// 根据指定比较器产生的顺序,返回给定collection的最小元素
static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp);
------ synchronized方法通过内部类继承Collection、Map接口,并重写里面的方法,定义Object mutex锁对象来实现对集合对象的操作的同步。注意在对集合进行迭代操作的时候,迭代代码块是要手动加锁。JDK给出如下示例。
// 返回一个线程安全的collection
static <T> Collection<T> synchronizedCollection(Collection<T> c);
// 返回一个线程安全的List
static <T> List<T> synchronizedList(List<T> list);
// 返回一个线程安全的Set
static <T> Set<T> synchronizedSet(Set<T> s);
// 返回一个线程安全的Map
static <K, V> Map<K, V> synchronizedMap(Map<K, V> m);
List集合常用操作
因为List集合具有元素可重复特点,相比其它类型集合少了对象的比较方法,在Collections工具类中为List集合提供了一些特有的比较方法。// 按自然顺序对指定的对象列表按升序进行排序
static <T extends Comparable<? super T>> void sort(List<T> list);
// 按指定比较器产生的顺序对指定List按升序进行排序
static <T extends Comparable<? super T>> void sort(LIst<T> list, Comparator<? super T> c);
// 使用二分查找,获取指定对象;使用对象比较器
static <T> binarySearch(List<? extends Comparable<? super T>> list, T key);
// 使用二分查找,获取指定对象;使用指定比较器
static <T> binarySearch(List<? extends T> list, T key, Comparator<? super T> c);
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class CollectionsDemo { public static void main(String args[]) { // 获取一个ArrayList List<String> list = getList(); System.out.println(list); // 根据自然顺序对指定的List集合按升序排序 Collections.sort(list); System.out.println("自然排序:" + list); // 获取List集合中指定元素的位置,使用对象比较器 int num = Collections.binarySearch(list, "bc"); System.out.println("num: " + num); // 根据比较器指定的顺序对List集合排序 Collections.sort(list, new StrLenComparator()); System.out.println("客户排序:" + list); // 获取List集合中指定元素的位置,使用指定比较器 num = Collections.binarySearch(list, "bc", new StrLenComparator()); System.out.println("num: " + num); } // 初始化一个List public static List<String> getList() { List<String> list = new ArrayList<String>(); list.add("a"); list.add("bc"); list.add("abc"); list.add("efgh"); list.add("bac"); return list; } } // 实现一个字符串按长度比较的类 class StrLenComparator implements Comparator<String> { public int compare(String s1, String s2) { int num = s1.length() - s2.length(); if (num == 0) { return s1.compareTo(s2); } return num; } }Collections类为List集合单独提供了排序和查找的方法,排序方法使用的还是对象的比较方法compareTo(),或者指定比较器;使用查找方法时一定要保证List集合是有序的,并且List集合的排序比较方式和查找方法中的比较方式一致(这是有二分查找原理决定的)。
Collections类常用方法
------ 获取Collection最值方法,当有多个相同最值时,只会返回其中一个对象// 根据元素的自然顺序,返回给定collection的最大元素
static <T extends Object & Comparable<? super T>> T max(collection<? extends T> coll);
// 根据指定比较器产生的顺序,返回给定collection的最大元素
static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp);
// 根据元素的自然顺序,返回给定collection的最小元素
static <T extends Object & Comparable<? super T>> T min(collection<? extends T> coll);
// 根据指定比较器产生的顺序,返回给定collection的最小元素
static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp);
------ synchronized方法通过内部类继承Collection、Map接口,并重写里面的方法,定义Object mutex锁对象来实现对集合对象的操作的同步。注意在对集合进行迭代操作的时候,迭代代码块是要手动加锁。JDK给出如下示例。
<span style="white-space:pre"> </span>Set s = Collections.synchronizedSet(new HashSet()); ... synchronized(s) { <span style="white-space:pre"> </span>Iterator i = s.iterator(); // Must be in the synchronized block while (i.hasNext()) foo(i.next()); <span style="white-space:pre"> </span>}还在下面强调:不遵从此建议将导致无法确定的行为。那是因为如果不锁定,在迭代操作时,其它线程修改了s的元素,就会导致迭代出错。由此也可看出Collections中的synchronized方法也不是绝对线程安全的。
// 返回一个线程安全的collection
static <T> Collection<T> synchronizedCollection(Collection<T> c);
// 返回一个线程安全的List
static <T> List<T> synchronizedList(List<T> list);
// 返回一个线程安全的Set
static <T> Set<T> synchronizedSet(Set<T> s);
// 返回一个线程安全的Map
static <K, V> Map<K, V> synchronizedMap(Map<K, V> m);
相关文章推荐
- ubuntu jdk1.7 安装
- [LeetCode][Java] Sudoku Solver
- 深入研究java.lang.Class类
- JAVA集合学习之——Properties类
- Java中的位运算符与基本数据类型
- eclipse常用快捷键
- java并发库之Executors常用的创建ExecutorService的几个方法说明
- java中的堆、栈、常量池
- 深入研究java.lang.ThreadLocal类
- Myeclipse快捷键大全
- spring mvc 异常统一处理方式
- Java调试
- Java 中的关键字 synchronized详解
- java与c#的区别(一)
- Java中普通代码块,构造代码块,静态代码块的代码示例及区分
- Java对象的序列化和反序列化
- 在Myeclipse中添加User Library,用户自己的库
- 水墨大写意笔法模拟简易方法
- 迄今为止最优的Eclipse运行性能调优 ,含eclipse.ini
- 使用javamail发信过程中的一些问题及解决方法