1. Collections工具类
- Collections是一个操作Set、List和Map等集合的工具类。
- Collectons中提供了一系列静态的方法对集合元素进行排序、查询、修改等操作,还提供了对集合对象设置不可变、对集合对象实现同步控制等方法。
- 排序操作:(均为static方法)
》reverse(List): 反转List中元素的顺序。
》shuffle(List): 对List 集合元素进行随机排序。
》sort(List): 根据元素的自然排序顺序对指定List集合元素按升序排序。
》sort(List,Comarator): 根据指定的Comparator产生的顺序对List集合元素进行排序。
》swap(List,int,int): 将指定list集合中的i处元素和j处元素进行交换。
2.Collections常用方法
- Object max(Collection): 根据元素的自然顺序,返回给定集合中的最大元素。
- Object max(Collection,Comparator): 根据Comparator指定的顺序,返回给定集合中的最大元素。
- Object min(Collection)
- Object min(Collection,Comparator)
- int frequency(Collection,Object): 返回指定集合中指定元素的出现次数。
- void copy(List dest,List src): 将src中的内容复制到dest中。
- boolean replaceAll(List list,Object oldVal,Object newVal): 使用新值替换List对象的所有旧制。
- copy(List dest,List src)的使用
List list=new ArrayList();
list.add(123);
list.add(43);
list.add(756);
list.add(-97);
List dest=Arrays.asList(new Object[list.size()]);
System.out.println(dest.size());
Collections.copy(dest,list);
System.out.println(dest);
- synchronizedCollection(Collection< T > c)
- synchronizedList(List< T > list)
- synchronizedMap(Map<K,V> m)
- synchronizedSet(Set< T > s)
- synchronizedSortedMap(SortedMap<K,V> m)
- synchronizedSortedSet(SortedSet< T > s)