有Person类如下:

 class Person {
  String name;
  int age;
  String address;
}

有main如下:

 import java.util.TreeSet;

 public class Test{
  public static void main(String[] args){
    Person p1 = new Person("AA",18,"CN");
    Person p2 = new Person("BB",18,"JP");
    Person p3 = new Person("CC",18,"CN");     TreeSet<Person> persons = new TreeSet<>();
    persons.add(p1);
    persons.add(p2);
    persons.add(p3);
    for (Object person : persons) {
      System.out.println(person);
    }
  }
}

由于TreeSet为可排序集合,所以要为存放对象(Person)指定排序规则。

排序规则:ASC(升序),age > address > name
故重写Person的compareTo():

 public int compareTo(Person o) {
if(this.age > o.age){
return 1;
}else if(this.age < o.age) {
return -1;
}
//判断age谁大,若相等,则判断address
int x = this.address.compareTo(o.address);
if(x != 0){
return x;
}
//判断address谁大,若相等,则判断name
return this.name.compareTo(o.name);
}

注:若要DESC排序:

  1、age比较中交换1/-1;
  2、address比较中return相反数值;
  3、name比较中return相反数值;

上述代码的逻辑顺序:
  传入比较对象o与自身进行对比;
    若this.age > o.age,返回“1”;
    若this.age < o.age,返回“-1”;
    若相等,比较address;
  令x = this.address.compareTo(o.address);
    若x != 0,即不相等,x即为要返回的值(1/-1);
    若x == 0,即相等,比较name;
  this.name.compareTo(o.name)的值即为要返回的值:
    若值为 1,即大于,
    若值为-1,即为小于,
    若值为 0,即三处均相等,则完全相等,不应存入。

完整代码:

 package toBKY;

 import java.util.TreeSet;

 public class Test{
public static void main(String[] args){
Person p1 = new Person("AA",18,"CN");
Person p2 = new Person("BB",18,"JP");
Person p3 = new Person("CC",18,"CN"); TreeSet<Person> persons = new TreeSet<>();
persons.add(p1);
persons.add(p2);
persons.add(p3); for (Object person : persons) {
System.out.println(person);
}
//使用for each 比Iterator代码量少 }
} class Person implements Comparable<Person>{
String name;
int age;
String address; public Person(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
} @Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
    '}';
  }   @Override
  public int compareTo(Person o) {
    if(this.age > o.age){
      return 1;
    }else if(this.age < o.age) {
      return -1;
    }
    //判断age谁大,若相等,则判断address
    int x = this.address.compareTo(o.address);
    if(x != 0){
      return x;
    }
    //判断address谁大,若相等,则判断name
    return this.name.compareTo(o.name);
  }
}

180217_JAVA学习_TreeSet中存放含多个String的类并设置排序规则的更多相关文章

  1. 无法解决 equal to 运算中 &quot;Chinese_PRC_CI_AS&quot; 和 &quot;SQL_Latin1_General_CP1_CI_AS&quot; 之间的排序规则冲突。

    什么是排序规则(collation) 关于SQL Server的排序规则,估计大家都不陌生,在创建数据库时我们经常要选择一种排序规则(conllation),一般我们会留意到每一种语言的排序规则都有许 ...

  2. 使用Spring JPA中Page、Pageable接口和Sort类完成分页排序

    显示时,有三个参数,前两个必填,第几页,一页多少个size,第三个参数默认可以不填. 但是发现这个方法已经过时了,通过查看它的源码发现,新方法为静态方法PageRequest of(page,size ...

  3. SQL Server 与MySQL中排序规则与字符集相关知识的一点总结

    字符集&&排序规则 字符集是针对不同语言的字符编码的集合,比如UTF-8字符集,GBK字符集,GB2312字符集等等,不同的字符集使用不同的规则给字符进行编码排序规则则是在特定字符集的 ...

  4. 近200篇机器学习&深度学习资料分享(含各种文档,视频,源码等)(1)

    原文:http://developer.51cto.com/art/201501/464174.htm 编者按:本文收集了百来篇关于机器学习和深度学习的资料,含各种文档,视频,源码等.而且原文也会不定 ...

  5. swing Jlable中存放变量显示问题

    java swing 学习 在做一个ATM机系统小案例中.碰到JLable中存放变量,变量发生改变.而JLable中还是显示原来的值,网上寻找答案,用updateUI()和revalidate();方 ...

  6. 学习sql中的排列组合,在园子里搜着看于是。。。

    学习sql中的排列组合,在园子里搜着看,看到篇文章,于是自己(新手)用了最最原始的sql去写出来: --需求----B, C, F, M and S住在一座房子的不同楼层.--B 不住顶层.C 不住底 ...

  7. [转]学习Nop中Routes的使用

    本文转自:http://www.cnblogs.com/miku/archive/2012/09/27/2706276.html 1. 映射路由 大型MVC项目为了扩展性,可维护性不能像一般项目在Gl ...

  8. 在主方法中定义一个大小为50的一维整型数组,数组i名为x,数组中存放着{1,3,5,…,99}输出这个数组中的所有元素,每输出十个换一行

    package hanqi; import java.util.Scanner; public class Test7 { public static void main(String[] args) ...

  9. 设单链表中存放n个字符,试设计一个算法,使用栈推断该字符串是否中心对称

    转载请注明出处:http://blog.csdn.net/u012860063 问题:设单链表中存放n个字符.试设计一个算法,使用栈推断该字符串是否中心对称,如xyzzyx即为中心对称字符串. 代码例 ...

随机推荐

  1. java文件上传-使用apache-fileupload组件

    目前文件上传的(框架)组件:Apache----fileupload .Orialiy – COS – 2008() .Jsp-smart-upload – 200M. 用fileupload上传文件 ...

  2. linux文件映射到windows(方便用虚拟机搭建linux服务器,用本地windows代码编辑)

    1,安装docker: https://docs.docker.com/install/linux/docker-ce/centos/ 2,linux上创建好需要共享的目录 /data/share(可 ...

  3. python 把一文件包含中文的字符写到另外文件乱码 UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position

    报错的代码是: file2 = open('target.txt','w')for line in open('test.txt'): file2.write(line)原因:文件编码不一致导致解决方 ...

  4. LNMT(Linux+Nginx+MySQL+Tomcat)常见性能参数调优

  5. wap尝试调取app(网易新闻为例)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. 仿制淘宝sku点击效果

    1.依赖jquery,主要利用二维数组. 2.原生手写. 代码如下: <!DOCTYPE html> <html> <head> <meta charset= ...

  7. rails 5.2 启动警告 warning: previous definition of VERSION was here和bootsnap

    bootsnap依赖问题 You should add gem 'bootsnap' to your gemfile to install it or remove the line require ...

  8. The version of SOS does not match the version of CLR you are debugging; SOS.dll版本不匹配; Dump文件不同环境mscordacwks.dll版本问题

    The version of SOS does not match the version of CLR you are debugging 和 PDB symbol for clr.dll not ...

  9. JS事件细分

    鼠标相关事件执行顺序 与 onmousedown 事件相关连得事件发生次序( 鼠标左侧/中间 按钮): onmousedown onmouseup onclick 与 onmousedown 事件相关 ...

  10. 1.1 JAVA装箱和拆箱以及Java Number & Math&Character 类

    JAVA装箱和拆箱 从Java SE5开始就提供了自动装箱的特性,如果要生成一个数值为10的Integer对象,只需要这样就可以了.原文链接: http://www.cnblogs.com/dolph ...