Student.java

package com.sxt.set3;
/*
* TreeSet:有序
* implements Comparable<Student>
* 如果用内部比较器TreeSet必须是实现Comparable接口来实现有序 否则会出现报错:com.sxt.set4.Student cannot be cast to java.lang.Comparable
* 使用泛型是为了在重写compareTo()方式时,object不用强制转换类型
*/
//内部比较器:在类内重写比较规则即compareTo()方法
public class Student implements Comparable<Student> {
private String name;
private int age;
private double salary;
public Student(String name, int age, double salary) {
super();
this.name = name;
this.age = age;
this.salary = salary;
}
public Student() {
super();
}
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;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
//内部比较器之排序规则 按年龄
@Override
public int compareTo(Student o) { return this.age - o.age;
} }

TestStudent.java

package com.sxt.set3;
/*
* TreeSet
*/
import java.util.Set;
import java.util.TreeSet; public class TestStudent {
public static void main(String[] args) {
Set<Student> arr = new TreeSet<>();
arr.add(new Student("bbb", 21, 532.2));
arr.add(new Student("ccc", 32, 32.2));
arr.add(new Student("ddd", 11, 352.2));
arr.add(new Student("aaa", 15, 32.2));
//遍历
for(Student s:arr){
System.out.println(s);
}
// 按照内部比较器按年龄排序结果:
// Student [name=ddd, age=11, salary=352.2]
// Student [name=aaa, age=15, salary=32.2]
// Student [name=bbb, age=21, salary=532.2]
// Student [name=ccc, age=32, salary=32.2] }
}

TreeSet的运用之使用内部比较器实现自定义有序(重要)的更多相关文章

  1. TreeSet之用外部比较器实现自定义有序(重要)

    Student.java package com.sxt.set5; public class Student{ private String name; private int age; priva ...

  2. Comparable内部比较器 和 Comparator外部比较器

    1:Comparable a:基本数据类型封装类都继承了Comparable接口 b:TreeSet 和TreeMap集合默认是按照自然顺序排序的 c:继承类中实现compareTo()方法,在类内部 ...

  3. TreeSet集合的自然排序与比较器排序、Comparable接口的compareTo()方法

    [自然排序] package com.hxl; public class Student implements Comparable<Student> { private String n ...

  4. TreeSet 比较器排序 自定义对象

    package cn.itcast.day21.treeset2; import java.util.Comparator; import java.util.TreeSet; /* * TreeSe ...

  5. AI-解析器-request.data内部如何按照自定义解析格式-解析数据

    QUESTION:post方法中调用request.data方法时,当在Courseview类中添加parser_classes=[ForParser,],就可以将数据解析成parser_classe ...

  6. JAVA集合四:比较器--类自定义排序

    参考链接: HOW2J.CN 前言 对于JAVA集合,都能够用集合的工具类Collections 提供的方法: Collections.sort(List list) Collections.sort ...

  7. 10.TreeSet、比较器

    Comparable和Comparator  Comparable 简介 Comparable 是排序接口.若一个类实现了Comparable接口,就意味着"该类支持排序".  即 ...

  8. TreeSet的两种实现方法:Comparable和Comparator(Java比较器)

    Comparable与Comparator实际上是TreeSet集合的两种实现方式,用来实现对象的排序.下边介绍一下两种比较器的使用方法和区别. Comparable称为元素的自然顺序,或者叫做默认顺 ...

  9. 零基础学习java------day15--------collections用法,比较器,Set(TreeSet,TreeMap),异常

    1. Collections用法 Collections: 集合的工具类public static <T> void sort(List<T> list) 排序,升序publi ...

随机推荐

  1. 利用Qt自带工具发布程序

    Qt官方开发环境生成的exe发布方式--使用windeployqt 从开始菜单-->Qt 5.4.0-->5.4-->MinGW 4.9 (32-bit)-->Qt 5.4 f ...

  2. writing-mode,文字竖直书写,字符之间距离,单词之间距离

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

  3. iscroll的滑动效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Linux 拷贝有更改的文件

    cp -Ruv /home/username/trunk_new/app/* /data/httpdocs/wwwroot/app/

  5. ios那些事之如何在ios5上运行gdb

    为啥要在ios上运行gdb? 这个问题见仁见智喽.对于搞开发的同学们来所, 有了gdb更方便跟踪分析别人的程序,取长补短:)这里不是教大家crack:) 运行环境: Mac OS 10.7.4 Xco ...

  6. java并发系列(六)-----Java并发:volatile关键字解析

    在 Java 并发编程中,要想使并发程序能够正确地执行,必须要保证三条原则,即:原子性.可见性和有序性.只要有一条原则没有被保证,就有可能会导致程序运行不正确.volatile关键字 被用来保证可见性 ...

  7. angular4 自定义表单组件

    自定义表单组件分为单值组件和多值组件. 单值组件:input/select/radio/textarea 多值组件:checkbox/tree组件 条件: 1.必须实现ControlValueAcce ...

  8. nginx链接末尾自动补全斜杠

    放在locaation里边就行 if (-d $request_filename){ rewrite ^(.*[^/])$ $/ permanent;#加斜杠 } 这样,nginx就会进行判断了,如果 ...

  9. 学习JDK1.8集合源码之--ArrayDeque

    1. ArrayDeque简介 ArrayDeque是基于数组实现的一种双端队列,既可以当成普通的队列用(先进先出),也可以当成栈来用(后进先出),故ArrayDeque完全可以代替Stack,Arr ...

  10. Javaweb项目中出现java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone.异常

    javaweb项目中java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represent ...