Collections类 解剖

来源:互联网 发布:linux内核设置 编辑:程序博客网 时间:2024/05/28 15:24

Collections是操作Collection集合对象的一个类,里面定义了静态函数,它是针对集合类的一个帮助类,此类不能实例化,他提供一系列static静态方法实现对各种集合的搜索、排序、线程安全化等操作。如:

Collections.shuffle(List<>list);  打乱list中元素的顺序

Collections.sort(List<>list,Comparator<>c);


区别于Collection,它是集合接口,继承它的接口有Set、List 。


实例:

public class TestCollections {public static void main(String[] args) {ArrayList list = new ArrayList();list.add(new Integer(102));list.add(new Integer(98));list.add(new Integer(38));list.add(new Integer(238));list.add(new Integer(9348));Collections.shuffle(list);for (Iterator iter = list.iterator(); iter.hasNext();) {System.out.println(iter.next() + " ");}Comparator com = Collections.reverseOrder();Collections.sort(list, com);System.out.println("THis is soted List with Collections  ");for (Iterator iter = list.iterator(); iter.hasNext();) {System.out.println(iter.next());}}}
输出:

102
38
238
98
9348
THis is soted List with Collections  
9348
238
102
98
38





0 0
原创粉丝点击