Collections工具类

发布时间 2023-11-20 12:18:31作者: 和哗

Collection工具类

Collections工具类,里面的方法全是静态方法.

1. 二分查找List

语法:

static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)  :使用二叉搜索算法搜索指定对象的指定列表

举个例子:

 1 public class Test {
 2     public static void main(String[] args) {
 3         //生成无参集合
 4         ArrayList<Student> list=new ArrayList<>();
 5         //添加元素
 6         list.add(new Student("张三",18));
 7         list.add(new Student("李四",29));
 8         list.add(new Student("王五",15));
 9         list.add(new Student("老刘",30));
10         list.add(new Student("李华",20));
11         //排序后的集合结果
12         Collections.sort(list,new Comparator<Student>() {
13             //使用匿名接口实现类的方式
14             @Override
15             public int compare(Student o1, Student o2) {
16                 return o1.getAge() - o2.getAge();
17             }
18         });
19         for (Student student:list) {
20             System.out.println(student);
21         }
22         //使用二分查找
23         int index = Collections.binarySearch(list, new Student("李华", 20), new Comparator<Student>() {
24             //使用匿名接口实现类的方式
25             @Override
26             public int compare(Student o1, Student o2) {
27                 return o1.getAge() - o2.getAge();
28             }
29         });
30         System.out.println("查找的元素在"+index+"位置");
31     }
32 }
33 class Student{
34     private String name;
35     private int age;
36 
37 
38     public Student() {
39     }
40 
41     public Student(String name, int age) {
42         this.name = name;
43         this.age = age;
44     }
45 
46     public String getName() {
47         return name;
48     }
49 
50     public void setName(String name) {
51         this.name = name;
52     }
53 
54     public int getAge() {
55         return age;
56     }
57 
58     public void setAge(int age) {
59         this.age = age;
60     }
61 
62     public String toString() {
63         return "Student{name = " + name + ", age = " + age + "}";
64     }
65 }

效果展示:

 2. 倒置集合

语法:

static void reverse(List<?> list):反转指定列表中元素的顺序

举个例子:

 1 public class Test {
 2     public static void main(String[] args) {
 3         //生成无参集合
 4         ArrayList<Student> list=new ArrayList<>();
 5         //添加元素
 6         list.add(new Student("张三",18));
 7         list.add(new Student("李四",29));
 8         list.add(new Student("王五",15));
 9         list.add(new Student("老刘",30));
10         list.add(new Student("李华",20));
11         System.out.println("反转前的集合");
12         for (Student student:list) {
13             System.out.println(student);
14         }
15         System.out.println("反转后的集合");
16         Collections.reverse(list);
17         for (Student student:list) {
18             System.out.println(student);
19         }
20     }
21 }
22 class Student{
23     private String name;
24     private int age;
25 
26 
27     public Student() {
28     }
29 
30     public Student(String name, int age) {
31         this.name = name;
32         this.age = age;
33     }
34 
35     public String getName() {
36         return name;
37     }
38 
39     public void setName(String name) {
40         this.name = name;
41     }
42 
43     public int getAge() {
44         return age;
45     }
46 
47     public void setAge(int age) {
48         this.age = age;
49     }
50 
51     public String toString() {
52         return "Student{name = " + name + ", age = " + age + "}";
53     }
54 }

效果展示:

 3. 洗牌功能

语法:

static void shuffle(List<?> list) :使用默认的随机源随机排列指定的列表

举个例子:

 1 public class Test {
 2     public static void main(String[] args) {
 3         //生成无参集合
 4         ArrayList<Student> list=new ArrayList<>();
 5         //添加元素
 6         list.add(new Student("张三",18));
 7         list.add(new Student("李四",29));
 8         list.add(new Student("王五",15));
 9         list.add(new Student("老刘",30));
10         list.add(new Student("李华",20));
11         System.out.println("洗牌前的集合");
12         for (Student student:list) {
13             System.out.println(student);
14         }
15         System.out.println("洗牌后的集合");
16         Collections.shuffle(list);
17         for (Student student:list) {
18             System.out.println(student);
19         }
20     }
21 }
22 class Student{
23     private String name;
24     private int age;
25 
26 
27     public Student() {
28     }
29 
30     public Student(String name, int age) {
31         this.name = name;
32         this.age = age;
33     }
34 
35     public String getName() {
36         return name;
37     }
38 
39     public void setName(String name) {
40         this.name = name;
41     }
42 
43     public int getAge() {
44         return age;
45     }
46 
47     public void setAge(int age) {
48         this.age = age;
49     }
50 
51     public String toString() {
52         return "Student{name = " + name + ", age = " + age + "}";
53     }
54 }

效果展示:

 4. 排序

语法:

static <T> void sort(List<T> list, Comparator<? super T> c) :根据指定的比较器引起的顺序对指定的列表进行排序

举个例子:

 1 public class Test {
 2     public static void main(String[] args) {
 3         //生成无参集合
 4         ArrayList<Student> list=new ArrayList<>();
 5         //添加元素
 6         list.add(new Student("张三",18));
 7         list.add(new Student("李四",29));
 8         list.add(new Student("王五",15));
 9         list.add(new Student("老刘",30));
10         list.add(new Student("李华",20));
11         System.out.println("排序后的集合");
12         Collections.sort(list, new Comparator<Student>() {
13             @Override
14             public int compare(Student o1, Student o2) {
15                 return o1.getAge()-o2.getAge();
16             }
17         });
18         for (Student student:list) {
19             System.out.println(student);
20         }
21         
22     }
23 }
24 class Student{
25     private String name;
26     private int age;
27 
28 
29     public Student() {
30     }
31 
32     public Student(String name, int age) {
33         this.name = name;
34         this.age = age;
35     }
36 
37     public String getName() {
38         return name;
39     }
40 
41     public void setName(String name) {
42         this.name = name;
43     }
44 
45     public int getAge() {
46         return age;
47     }
48 
49     public void setAge(int age) {
50         this.age = age;
51     }
52 
53     public String toString() {
54         return "Student{name = " + name + ", age = " + age + "}";
55     }
56 }

效果展示:

 5. 把线程不安全的集合转化为线程安全的集合

在未来多线程操作的时候,线程安全就会显得十分重要.

语法:

static <T> List<T> synchronizedList(List<T> list) :返回由指定列表支持的同步(线程安全)列表。 

举个例子:

 1 public class Test {
 2     public static void main(String[] args) {
 3         //生成无参集合
 4         ArrayList<Student> list=new ArrayList<>();
 5         //添加元素
 6         list.add(new Student("张三",18));
 7         list.add(new Student("李四",29));
 8         list.add(new Student("王五",15));
 9         list.add(new Student("老刘",30));
10         list.add(new Student("李华",20));
11       12         List<Student> students = Collections.synchronizedList(list);
13 14         for (Student student:students) {
15             System.out.println(student);
16         }
17 
18     }
19 }
20 class Student{
21     private String name;
22     private int age;
23 
24 
25     public Student() {
26     }
27 
28     public Student(String name, int age) {
29         this.name = name;
30         this.age = age;
31     }
32 
33     public String getName() {
34         return name;
35     }
36 
37     public void setName(String name) {
38         this.name = name;
39     }
40 
41     public int getAge() {
42         return age;
43     }
44 
45     public void setAge(int age) {
46         this.age = age;
47     }
48 
49     public String toString() {
50         return "Student{name = " + name + ", age = " + age + "}";
51     }
52 }

效果展示:

 多线程无法直接展示出来,需要学习到线程时才能完美的展示效果。