ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

JAVA Collections

2022-05-06 20:02:21  阅读:118  来源: 互联网

标签:JAVA List void list Collections Student public


 

Collenctions概述和使用

 

Collections类的概述

    • 是针对集合操作的工具类


Collections类的常用方法

    • public static <T extends Comparable<? super T>> void sort(List list):将指定的列表按升序排序
    • public static void reverse(List<?> list):反转指定列表中元素的顺序
    • public static void shuffle(List<?> list):使用默认的随机源随机排列指定的列表

  

 1 import java.util.ArrayList;
 2 import java.util.Collections;
 3 import java.util.List;
 4 
 5 /*
 6 Collections类的概述
 7 
 8 - 是针对集合操作的工具类
 9 
10 Collections类的常用方法
11 
12 - public static <T extends Comparable<? super T>> void sort(List<T> list):将指定的列表按升序排序
13 - public static void reverse(List<?> list):反转指定列表中元素的顺序
14 - public static void shuffle(List<?> list):使用默认的随机源随机排列指定的列表
15 
16 
17 
18 */
19 
20 public class CollectionsDemo {
21     public static void main(String[] args) {
22         //创建List集合对象
23         List<Integer> list = new ArrayList<Integer>();
24 
25         //添加元素
26         list.add(10);
27         list.add(20);
28         list.add(40);
29         list.add(60);
30         list.add(50);
31 
32         //        public static <T extends Comparable<? super T>> void sort(List<T> list):将指定的列表按升序排序
33         Collections.sort(list);
34         /*运行结果:
35         [10, 20, 40, 50, 60]
36         */
37 
38         //        public static void reverse(List<?> list):反转指定列表中元素的顺序
39         Collections.reverse(list);
40         /*运行结果:反转列表中元素的顺序
41         [50, 60, 40, 20, 10]
42         */
43 
44         //        public static void shuffle(List<?> list):使用默认的随机源随机排列指定的列表
45         Collections.shuffle(list);
46         /*运行结果:随机排序  每次运行会出现不同的结果
47         [10, 60, 20, 50, 40]
48         */
49 
50         System.out.println(list);
51 
52     }
53 }

 

案例:ArrayList存储学生对象并排序

需求:ArratLIst存储学生对象,使用Collections对ArrayList进行排序
要求:按照年龄从小到大的排序,年龄相同时,按照姓名的字母顺序排序

  

 1 //定义学生类
 2 public class Student {
 3     private String name;
 4     private int age;
 5 
 6     public String getName() {
 7         return name;
 8     }
 9 
10     public void setName(String name) {
11         this.name = name;
12     }
13 
14     public int getAge() {
15         return age;
16     }
17 
18     public void setAge(int age) {
19         this.age = age;
20     }
21 
22     public Student(String name, int age) {
23         super();
24         this.name = name;
25         this.age = age;
26     }
27 
28     public Student() {
29         super();
30     }
31 
32 }

 

 1 import java.util.ArrayList;
 2 import java.util.Collections;
 3 import java.util.Comparator;
 4 
 5 /*
 6 案例:ArrayList存储学生对象并排序
 7 
 8 需求:ArratLIst存储学生对象,使用Collections对ArrayList进行排序
 9     要求:按照年龄从小到大的排序,年龄相同时,按照姓名的字母顺序排序
10 
11 */
12 public class ArrayListDemo {
13     public static void main(String[] args) {
14         //创建ArrayList集合对象
15         ArrayList<Student> array = new ArrayList<Student>();
16 
17         //创建学生对象
18         Student s1 = new Student("小白", 12);
19         Student s2 = new Student("小黑", 11);
20         Student s3 = new Student("小蓝", 14);
21         Student s4 = new Student("小红", 15);
22 
23         //把学生添加到集合中
24         array.add(s1);
25         array.add(s2);
26         array.add(s3);
27         array.add(s4);
28 
29         //使用Collections对集合排序
30         //sort (List<T> list,Comparator<? super T>)
31         Collections.sort(array, new Comparator<Student>() {
32             @Override
33             public int compare(Student s1, Student s2) {
34                 //按照年龄从小到大的排序
35                 int num = s1.getAge() - s2.getAge();
36                 //年龄相同时,按照姓名的字母顺序排序
37                 int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
38                 return num2;
39             };
40         });
41 
42         //遍历集合
43         for (Student s : array) {
44             System.out.println(s.getName() + "," + s.getAge());
45         }
46 
47     }
48 }

 

标签:JAVA,List,void,list,Collections,Student,public
来源: https://www.cnblogs.com/cuipengchong/p/16230203.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有