一、基本介绍:
1、Collections工具类:
(1)Collections是一个操作Set、List和Map等集合的工具类
(2)Collections中提供了一系列静态的方法对集合元素进行排序、查询和修改等操作
2、排序操作:(均为static方法)
(1)reverse(List):反转List中元素的顺序
(2)shuffle(List):对List集合元素进行随机排序
(3)sort(List):根据元素的自然顺序对指定List集合元素按升序排序(按照首字母的前后顺序)
(4)sort(List,Comparator):根据指定的Comparator产生的顺序对List集合元素进行排序
(5)swap(List,int,int):将指定list集合中的i处元素和j处元素进行交换
- package Collection_;
-
- import java.util.*;
-
- @SuppressWarnings({"all"})
- public class Collections_ {
- public static void main(String[] args) {
- List list=new ArrayList();
- list.add("tom");
- list.add("smith");
- list.add("king");
- list.add("milan");
-
- Collections.reverse(list);
- System.out.println("list="+list);
- //list=[milan, king, smith, tom]
-
- System.out.println("=======================");
- for(int i=0;i<5;i++) {
- Collections.shuffle(list);
- System.out.println("list=" + list);
- }
- //list=[milan, king, tom, smith]
- //list=[smith, tom, milan, king]
- //list=[milan, tom, king, smith]
- //list=[milan, tom, king, smith]
- //list=[tom, smith, milan, king]
-
- System.out.println("=======================");
- Collections.sort(list);
- System.out.println("list=" + list);
- //list=[king, milan, smith, tom]
-
- //sort(List.Comparator):根据指定的Comparator产生的顺序对List集合元素进行排序
- //1、按照字符串的长度大小排序
- System.out.println("=======================");
- Collections.sort(list,new Comparator(){
- @Override
- public int compare(Object o1,Object o2){
- return ((String)o1).length()-((String)o2).length();
- }
- });
- System.out.println("list=" + list);
- //list=[tom, king, milan, smith]
-
- System.out.println("=======================");
- Collections.swap(list,0,1);
- System.out.println("list=" + list);
- //list=[king, tom, milan, smith]
- }
- }
3、 查找、替换
1)object max(Collection):根据元素的自然顺序(首字母顺序),返回给定集合中的最大元素
2)Object max(Collection,Comparator):根据Comparator指定的顺序,返回给定集合中的最大元素
3) Object min(Collection)
4)Object min(Collection,Comparator)
5)int frequency(Collection,Object):返回指定集合中指定元素的出现次数
6)void copy(Listdest,Listsrc):将src中的内容复制到dest中
7)boolean replaceAlI(List list,Object oldVal,Object newVal):使用新值替换List对象的所有旧值
- package Collection_;
-
- import java.util.*;
-
- @SuppressWarnings({"all"})
- public class Collections_ {
- public static void main(String[] args) {
- List list=new ArrayList();
- list.add("tom");
- list.add("smith");
- list.add("king");
- list.add("milan");
- System.out.println("list="+list);
- //list=[tom, smith, king, milan]
-
- System.out.println("===============================");
- System.out.println(Collections.max(list));
- //tom
-
- System.out.println("===============================");
- Object maxObject=Collections.max(list,new Comparator(){
- public int compare(Object o1,Object o2){
- return ((String)o1).length()-((String)o2).length();
- }
- });
- System.out.println(maxObject);
- //smith
-
- System.out.println("===============================");
- System.out.println(Collections.frequency(list,"tom"));
- //1
-
- System.out.println("===============================");
- ArrayList dest=new ArrayList();
- //源码:int srcSize = src.size();初始时,dest为null,null.size()会抛出异常
- for(int i=0;i
- dest.add("");
- }
- //为了完成一个完整拷贝,我们需要先dest赋值,大小和list.size()一样
- Collections.copy(dest,list);
- System.out.println("dest="+dest);
- //dest=[tom, smith, king, milan]
-
- System.out.println("===============================");
- Collections.replaceAll(list,"tom","汤姆");
- System.out.println("list="+list);
- //list=[汤姆, smith, king, milan]
- }
- }