1、容器TreeMap,默认根据Key对象中某个属性的从小到大排列元素。

(1)如下代码示例,Key是整型数字,所以按照其从小到大的顺序排列

public class TestTreeMap {
public static void main(String[] args){
TreeMap<Integer,String> treeMap = new TreeMap<>();
treeMap.put(100,"1号");
treeMap.put(102,"2号");
treeMap.put(101,"3号");
System.out.println(treeMap);
}
}
/*output:
{100=1号, 101=3号, 102=2号}
*/

(2)当Key是自定义类的实例时,自定义类要实现Comparable<T>接口,重写compareTo(T)方法;

主要工作就是在自定义类中定义一个可以比较的属性,同时实现比较方法compareTo:

public class TestTreeMap {
public static void main(String[] args){
Person person1 = new Person(100,"张",5000);
Person person2 = new Person(102,"王",6000);
Person person3 = new Person(101,"李",7000);
TreeMap<Person,String> treeMap1 = new TreeMap<>();
treeMap1.put(person1,"一班");
treeMap1.put(person2,"二班");
treeMap1.put(person3,"三班"); Set<Map.Entry<Person,String>> set = treeMap1.entrySet();
for (Map.Entry<Person, String> entry : set) {
System.out.print(entry.getKey().id+"="+ entry.getValue()+'\t');
}
}
}
class Person implements Comparable<Person>{
public int id;
public String name;
public int salary; public Person(int id, String name, int salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public Person(){ }
public int compareTo(Person e){
if (this.id > e.id){
return 1;
}else if(this.id < e.id){
return -1;
}else{
return 0;
}
}
}
/*output:
100=一班 101=三班 102=二班
*/

2、TreeSet和TreeMap类似,因为TreeSet的底层也是TreeMap实现,TreeSet存储的元素,底层是存放在TreeMap的Key位置

即TreeSet存储自定义类的对象时,也需要将自定义类实现Comparable<T>接口,此处分析省略

Comparable接口——容器中自定义类排序的更多相关文章

  1. VBA中自定义类和事件的(伪)注册

    想了解一下VBA中自定义类和事件,以及注册事件处理程序的方法. 折腾了大半天,觉得这样的方式实在称不上“注册”,所以加一个“伪”字.纯粹是瞎试,原理也还没有摸透.先留着,有时间再接着摸. 做以下尝试: ...

  2. C#中自定义类数组和结构数组的使用

    如有雷同,不胜荣幸,若转载,请注明 C#中自定义类数组和结构数组的使用 最近在很多项目中发现很多时候给定的数组要实现某个逻辑或处理很是麻烦,一维数组,二维数组,,,等等需要经过n多转换,还不如自己写一 ...

  3. Python中自定义类未定义__lt__方法使用sort/sorted排序会怎么处理?

    在<第8.23节 Python中使用sort/sorted排序与"富比较"方法的关系分析>中介绍了排序方法sort和函数sorted在没有提供key参数的情况下默认调用 ...

  4. 十三、实现Comparable接口和new Comparator<T>(){ }排序的实现过程

    参考:https://www.cnblogs.com/igoodful/p/9517784.html Collections有两种比较规则方式,第一种是使用自身的比较规则: 该类必须实现Compara ...

  5. java通过Comparable接口实现字符串比较大小排序的简单实例

    /** * 对象比较大小compare的用法 字符串排序 * 练习代码, 给定字符串" nba" "cba" "ncaa" "wb ...

  6. ExtJS 中自定义类

    首先我们来看一看在Javascript中,是怎样自定义类的: var Person = function (name, age) { this.Name = ""; this.Ag ...

  7. (转).Net中自定义类作为Dictionary的key详解

    在定义数据结构时,Dictionary提供了快速查找数据的功能,另外Dictionary< TKey, TValue >属于key-value键值对数据结构,提供了泛型的灵活性,是数据结构 ...

  8. MFC中 自定义类访问主对话框控件的方法

    之前一直在找有木有好点的方法.现在终于被我找到,收藏之~~~~~~ 在使用mfc的时候经常遇到自定义类访问主对话框控件的问题,例如自定义类中的方法要输出一段字符串到主对话框的EDIT控件.控制对话框的 ...

  9. Python中自定义类如果重写了__repr__方法为什么会影响到str的输出?

    这是因为Python3中,str的输出是调用类的实例方法__str__来输出,如果__str__方法没有重写,则自动继承object类的__str__方法,而object类的__str__方法是调用_ ...

随机推荐

  1. NPOI导入导出Excel数据

    代码: using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; usi ...

  2. Connecting to MQSeries with .NET

    By connecting to MQSeries withing a .NET application, first it has to be done is to install MQ Serie ...

  3. Loadrunner脚本回放无法准确定位欲删除元素

    Loadrunner脚本回放无法准确定位欲删除元素 问题: loadrunner脚本回放无法准确定位欲删除元素 详细: 我司ocrm系统,我的工作台菜单->我的综合工作台子页面下,工作日志页面删 ...

  4. JMeter Ant Task 生成的*.jtl打开之后request和response data是空的,怎样让其不是空的呢?

    JMeter Ant Task 生成的*.jtl打开之后request和response data是空的,怎样让其不是空的呢?修改JMeter.properties,将jmeter.save.save ...

  5. C变参数函数demo

    #include <stdio.h> #include <stdarg.h> int sum(int a,...) {     int temp = 0,sum=0,count ...

  6. Linux shell脚本的字符串截取

    http://blog.csdn.net/gumanren/article/details/5601544 Linux 的字符串截取很有用.有八种方法. 假设有变量 var=http://www.ha ...

  7. KBMMW 4.6 正式版发布

    喜大普奔迎新年! Merry Christmas! We are happy to announce the release of kbmMW v. 4.60.00 Professional and ...

  8. 521. Longest Uncommon Subsequence I

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  9. hdu-1128(数学问题,筛数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1128 思路:从0,开始,每次求一个数x的d(x),然后判断如果x没有标记,则说明x没有由任意一个d(i ...

  10. Django入门指南-第7章:模板引擎设置(完结)

    http://127.0.0.1:8000/ <!--templates/home.html--> <!DOCTYPE html> <html> <head& ...