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. Redis源码解析:26集群(二)键的分配与迁移

    Redis集群通过分片的方式来保存数据库中的键值对:一个集群中,每个键都通过哈希函数映射到一个槽位,整个集群共分16384个槽位,集群中每个主节点负责其中的一部分槽位. 当数据库中的16384个槽位都 ...

  2. Perseus-BERT——业内性能极致优化的BERT训练方案

    一,背景——横空出世的BERT全面超越人类 2018年在自然语言处理(NLP)领域最具爆炸性的一朵“蘑菇云”莫过于Google Research提出的BERT(Bidirectional Encode ...

  3. Ubuntu 18.04 美化

    Ubuntu 18.04 美化 sudo apt install gnome-tweak-tool sudo apt install gnome-shell-extensions sudo apt i ...

  4. Centos 设置时区

    参考网址: http://jingyan.baidu.com/article/636f38bb268a82d6b84610bd.html //打开设置 tzselect //选择 )Asia → )c ...

  5. 将html保存为图片(电脑端)

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

  6. python实例 输出你好

    #打开新窗口,输入: #! /usr/bin/python # -*- coding: utf8 -*- s1=input("Input your name:") print(&q ...

  7. Leetcode669.Trim a Binary Search Tree修建二叉树

    给定一个二叉搜索树,同时给定最小边界L 和最大边界 R.通过修剪二叉搜索树,使得所有节点的值在[L, R]中 (R>=L) .你可能需要改变树的根节点,所以结果应当返回修剪好的二叉搜索树的新的根 ...

  8. chrome 浏览器 添加访问助手来访问网上应用商店

    chrome浏览器的强大之处,在于可以chrome浏览器的扩展程序来实现很多功能.然而不能下载扩展程序.可以借助chrome访问助手来实现: 下载chrome访问助手:https://pan.baid ...

  9. Codeforces Round #189 (Div. 2) A. Magic Numbers【正难则反/给出一个数字串判断是否只由1,14和144组成】

    A. Magic Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  10. 调用Lua脚本print(xxx)报attempt to call a nil value (global 'print')错误

    在自己程序里调用Lua脚本print(xxx) 报出attempt to call a nil value (global 'print')错误 解决方法: luaopen_base(L); 或者 l ...