(JAVA)红黑树之自然顺序排序和自定义排序方式

package cn.book.objectarr;/*** @author alina* @date 2021年08月22日 6:57 下午*/
public class Student implements Comparable<Student> {private String name;private int age;public Student(){}/***** @author Alina* @date 2021/9/21 9:41 下午* @param o* @return int* 使用构造方法的自然排序方式*/public  int compareTo(Student o ){//String类自带comparaTo();int num = this.name.compareTo(o.name);return num==0?this.age - o.age:num;
//        if(num==0){
//            return this.age - o.age;
//        }else{
//            return num;
//        }}public Student(String name,int age){this.age = age;this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return this.age;}public void setAge(int age) {this.age = age;}public String toString(){return "Student"+" "+name+" "+age;}public boolean equals(Object o){if (o == null) {return false;}else if(this == o){return true;}else if (o instanceof Student){Student s = (Student) o;return this.name.equals(s.name) && this.age == s.age;}return false;}public int hasCode(){return name.hashCode() +age*39;}}

public class TreeSetDemo {public static void main(String[] args) {//以姓名为顺序排序;method_1();//以年龄为顺序排序method_2();}public static void method_1(){TreeSet<Student> tr =  new TreeSet<>();tr.add(new Student("zhangsan",12));tr.add(new Student("lisi",13));tr.add(new Student("wanger",56));tr.add(new Student("lisi",14));Iterator it = tr.iterator();while (it.hasNext()){System.out.println(it.next());}}

package cn.book.objectarr;import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;/*** @author Alina* @date 2021年09月21日 8:50 下午*/
class myCompatato implements Comparator<Student>{@Overridepublic int compare(Student o1, Student o2) {int age = o1.getAge() - o2.getAge();return age == 0? o1.getName().compareTo(o2.getName()):age;}
}
public class TreeSetDemo {public static void main(String[] args) {//以年龄为顺序排序method_2();}public static void method_2(){TreeSet<Student> tr =  new TreeSet<>( new myCompatato());tr.add(new Student("zhangsan",12));tr.add(new Student("lisi",13));tr.add(new Student("wanger",56));tr.add(new Student("lisi",14));Iterator it = tr.iterator();while (it.hasNext()){System.out.println(it.next());}}}

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421758.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php正则表达式如何找到匹配模式中的最后一组

转载于:https://www.cnblogs.com/MyFlora/archive/2013/06/07/3124073.html

苏教版国标本小学语文第一册汉字笔画

苏教版国标本小学语文第一册汉字笔画 转载于:https://www.cnblogs.com/shangdawei/archive/2013/06/09/3129240.html

我的学习生涯(Delphi篇) - 21

我们平常要和图片打交道&#xff0c;那么我们如何把图片存在数据库中呢&#xff1f; -------------------------------------------------------------------------------------------------美丽分割线--------------------------- 年代&#xff1a;2007 文件&#xff1a;My091…

WordPress的RSS订阅优化

一、确定一个永久的RSS地址 WordPress默认的订阅地址是www.domain.com/feed&#xff0c;使用这样的地址有三个比较大的缺陷&#xff1a; 1、无法统计多少人订阅了你。 2、博客更换域名或者被GFW后原RSS地址不能访问了&#xff0c;原来的读者群就流失了。 3、输出的内容太单调…

redis数据批量导入导出

针对工作中可能用到 将某台服务器中的redis数据 导出然后导入到新的服务器中&#xff0c;一种方法是redis-dump工具&#xff0c;但是 他需要安装ruby环境&#xff0c;安装环境的过程中还可能出现意想不到的错误。所以不得不选用其他方法了。一下 是几点思路 供参考。在此谢谢我…