TreeSet的运用之使用内部比较器实现自定义有序(重要)
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的运用之使用内部比较器实现自定义有序(重要)的更多相关文章
- TreeSet之用外部比较器实现自定义有序(重要)
Student.java package com.sxt.set5; public class Student{ private String name; private int age; priva ...
- Comparable内部比较器 和 Comparator外部比较器
1:Comparable a:基本数据类型封装类都继承了Comparable接口 b:TreeSet 和TreeMap集合默认是按照自然顺序排序的 c:继承类中实现compareTo()方法,在类内部 ...
- TreeSet集合的自然排序与比较器排序、Comparable接口的compareTo()方法
[自然排序] package com.hxl; public class Student implements Comparable<Student> { private String n ...
- TreeSet 比较器排序 自定义对象
package cn.itcast.day21.treeset2; import java.util.Comparator; import java.util.TreeSet; /* * TreeSe ...
- AI-解析器-request.data内部如何按照自定义解析格式-解析数据
QUESTION:post方法中调用request.data方法时,当在Courseview类中添加parser_classes=[ForParser,],就可以将数据解析成parser_classe ...
- JAVA集合四:比较器--类自定义排序
参考链接: HOW2J.CN 前言 对于JAVA集合,都能够用集合的工具类Collections 提供的方法: Collections.sort(List list) Collections.sort ...
- 10.TreeSet、比较器
Comparable和Comparator Comparable 简介 Comparable 是排序接口.若一个类实现了Comparable接口,就意味着"该类支持排序". 即 ...
- TreeSet的两种实现方法:Comparable和Comparator(Java比较器)
Comparable与Comparator实际上是TreeSet集合的两种实现方式,用来实现对象的排序.下边介绍一下两种比较器的使用方法和区别. Comparable称为元素的自然顺序,或者叫做默认顺 ...
- 零基础学习java------day15--------collections用法,比较器,Set(TreeSet,TreeMap),异常
1. Collections用法 Collections: 集合的工具类public static <T> void sort(List<T> list) 排序,升序publi ...
随机推荐
- agc033
A题意:给你个黑白矩阵,每次黑的周围一圈会变黑,求多少次之后全黑.n <= 1000 解:BFS即可. #include <bits/stdc++.h> ; , , -, }; , ...
- 前端(jQuery)(9)-- jQuery菜单
1.垂直菜单布局 2.垂直菜单实现 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- warning: deprecated conversion from string constant to 'char*
warning: deprecated conversion from string constant to 'char* #include<iostream> using namespa ...
- Activity、FragmentActivity和AppCompatActivity的区别
Activity Activity是最基础的一个,是其它类的直接或间接父类. Activity中只能使用系统自带的host Fragment(API Level 11中加入),对应getFragmen ...
- git图形化
在windows下安装git中文版客户端并连接gitlab 转载自:https://blog.whsir.com/post-1801.html 下载git Windows客户端 git客户端下载地址: ...
- python Pandas文件读写
- golang之结构体
Go 语言使用结构体和结构体成员来描述真实世界的实体和实体对应的各种属性. Go语言通过用自定义的方式形成新的类型,结构体是类型中带有成员的复合类型. Go 语言中的类型可以被实例化,使用new或&a ...
- 字符串的trim()用法
trim() 方法会从一个字符串的两端删除空白字符.在这个上下文中的空白字符是所有的空白字符 (space, tab, no-break space 等) 以及所有行终止符字符(如 LF,CR). ...
- Hdu 3068 最长回文字串Manacher算法
题目链接 最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- webserver的性能问题,一语道破真谛
一直纠结我们要大减的paas平台需要支持多大的并发数. 看到一个网友所说的,恍然大悟,按原有我的理解和要求,并发达到w级 req/s,已经是非常高的要求了,单纯从软件上是很难实现的,一定要以来硬件上的 ...