[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>自然排序Comparator的使用
*
存储学生对象并遍历,创建TreeSet集合使用无参构造方法
*
要求:按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
创建学生类:
package com.gather.set.treeset; public class Student implements Comparable<
Student>{ private String name; private int age; public Student() { } public
Student(String name, int age) { this.name = name; this.age = age; } public
String getName() { return name; } public void setName(String name) { this.name =
name; } public int getAge() { return age; } public void setAge(int age) { this.
age= age; } @Override public int compareTo(Student s) { /*//return 0;重复元素
//return 1;正数,升序存储 return -1;//负数,降序存储*/ //按照年龄从小到大排序 int num = this.age - s.age
();//升序 //年龄相同时,按照姓名的字母顺序排序 int num2 = num == 0 ? this.name.compareTo(s.name) :
num; return num2; } }
测试类:
package com.gather.set.treeset; import java.util.TreeSet; public class
TreeSetDemo02 { public static void main(String[] args) { TreeSet<Student> ts =
new TreeSet<Student>(); Student s1 = new Student("xishi", 29); Student s2 = new
Student("wangzhaojun", 28); Student s3 = new Student("diaochan", 30); Student s4
= new Student("yangyuhuan", 33); Student s5 = new Student("lisi", 33); ts.add(s1
); ts.add(s2); ts.add(s3); ts.add(s4); ts.add(s5); //遍历 for (Student s : ts) {
System.out.println(s.getName() + "," + s.getAge()); } } }
自然排序要求自定义实现java.lang.Comparable接口并且重写ComparaTo()方法,按要求使用ComparaTo()进行比较。
要求年龄从小到大排序,比较年龄的大小当返回值为0时,表明学生的年龄是相同的,当年龄相同时接着按照姓名的字母顺序进行排序;当返回值为1时,就会进行升序存储即从低到高的顺序;当
返回值为-1时,就会降序存储即从高到低的顺序。