java 集合工具类_java集合工具类

1 packagecol;2

3 public class Student implementsComparable {4 private intid;5 privateString name;6 private intage;7 privateString city;8 publicStudent() {9 }10 public Student(int id, String name, intage, String city) {11 super();12 this.id =id;13 this.name =name;14 this.age =age;15 this.city =city;16 }17 public intgetId() {18 returnid;19 }20 public void setId(intid) {21 this.id =id;22 }23 publicString getName() {24 returnname;25 }26 public voidsetName(String name) {27 this.name =name;28 }29 public intgetAge() {30 returnage;31 }32 public void setAge(intage) {33 this.age =age;34 }35 publicString getCity() {36 returncity;37 }38 public voidsetCity(String city) {39 this.city =city;40 }41 @Override42 publicString toString() {43 return "Student [id=" + id + ", name=" + name + ", age=" + age + ", city=" + city + "]";44 }45 //重写比较器接口中的方法

46

47 /*

48 {1,3,5,7 }升序49 {7,5,3,1 }降序50 //输入源 3 1 7 551 {3}52

53 返回值:54 1正数:当前对象大, 降序55 -1负数:传入的对象大 ,升序56 0:一样大57 */

58

59 public intcompareTo(Object o) {60 Student inputstudent=(Student)o;61 //根据学号排序,降序

62 int result =this.id>inputstudent.id?-1:( this.id == inputstudent.id ?0:1);

//int result =this.id

68 }69 }70

71 packagecol;72

73 public classTeacher {74 private intid;75 privateString name;76 private intage;77 privateString city;78 public Teacher(int id, String name, intage, String city) {79 this.id =id;80 this.name =name;81 this.age =age;82 this.city =city;83 }84 publicTeacher() {85 super();86 }87 public intgetId() {88 returnid;89 }90 public void setId(intid) {91 this.id =id;92 }93 publicString getName() {94 returnname;95 }96 public voidsetName(String name) {97 this.name =name;98 }99 public intgetAge() {100 returnage;101 }102 public void setAge(intage) {103 this.age =age;104 }105 publicString getCity() {106 returncity;107 }108 public voidsetCity(String city) {109 this.city =city;110 }111 @Override112 publicString toString() {113 return "Teacher [id=" + id + ", name=" + name + ", age=" + age + ", city=" + city + "]";114 }115

116 }117

118 packagecol;119

120 importjava.util.Comparator;121

122 /*思路:将比较的对象(Student)实现Comparable接口,123 * 重写连的compareTo()方法。在compareTo()中编写比较的逻辑。重点是返回值,-1 ,0,1 ;124 Comparator:外部比较器 (无侵入性,不影响原有代码)125 先定义一个外部比较器*/

126 public class MyComparatorWithId implementsComparator {127

128 @Override129 public intcompare(Object o1, Object o2) {130 Teacher t1=(Teacher)o1;131 Teacher t2=(Teacher)o2;132 return t2.getId()-t1.getId();133 }134

135

136 }137

138 packagecol;139

140 importjava.util.ArrayList;141 importjava.util.Collections;142 importjava.util.List;143

144 public classDemo8 {145 //内部比较器

146 public static voidmain(String[] args) {147 List students = new ArrayList<>();148 Student s1 = new Student(10, "张三", 23, "阜阳");149 Student s2 = new Student(15, "李四", 21, "亳州");150 Student s3 = new Student(13, "王二", 26, "宿州");151 Student s4 = new Student(17, "麻子", 65, "合肥");152 students.add(s1);153 students.add(s2);154 students.add(s3);155 students.add(s4);156 Collections.sort(students);157 System.out.println(students);158 System.out.println("-------");159

160 Teacher t1=new Teacher(12, "贤明", 56, "山东");161 Teacher t2=new Teacher(42, "大明", 16, "贵州");162 Teacher t3=new Teacher(13, "小明", 76, "四川");163 List teachers=new ArrayList();164 teachers.add(t1);165 teachers.add(t2);166 teachers.add(t3);167 Collections.sort(teachers, newMyComparatorWithId());168 System.out.println(teachers);169 }170

171 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值