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 ...
随机推荐
- MySQL系列(六)--索引优化
在进行数据库查询的时候,索引是非常重要的,当然前提是达到一定的数据量.索引就像字典一样,通过偏旁部首来快速定位,而不是一页页 的慢慢找. 索引依赖存储引擎层实现,所以支持的索引类型和存储引擎相关,同一 ...
- SpringMVC + Mybatis + Shiro + ehcache时缓存管理器报错。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroFilter' ...
- 【python之路28】模块python与excel
一.可使用的第三方库 python中处理excel表格,常用的库有xlrd(读excel)表.xlwt(写excel)表.openpyxl(可读写excel表)等.xlrd读数据较大的excel表时效 ...
- [jnhs]未完待续HttpServletRequest示例
uri: /kaihu/showip.html method: GET QueryString: null Parameters: Headers: Name: host Value: localho ...
- Jboss 默认加载项目访问
修改JBOSS的server.xml路径为: D:\Program Files\jboss-4.2.2.GA\server\default\deploy\jboss-web.deployer\serv ...
- 老大让我看baidu他们的查公交是怎么做的,我就看了
突然发现,baidu的查公交,Json请求都很乱 朝阳公园西门3号门 人民大学 较快捷 http://map.baidu.com/?newmap=1&reqflag=pcmap&biz ...
- Fitnesse批量读取变量信息,并保存到用例执行上下文中
Fitnesse变量可以分成两种,一种是自定义变量,另一种是用例执行过程中的临时变量. 在Finesse使用过程中,如果需要定义一些公共的变量,可以统一在一个文件中使用自定义变量的方法,将公共变量全部 ...
- 【python之路9】类型定义与转换
一.整型(int),int的作用 1.创建int类型并赋值 n = 10 或者 n = int(10) #只要是类名加括号,都会去执行类中的 __init__()方法 n = 10,实际内部会去执 ...
- HDU 1690 最短路
#include<stdio.h> #include<string.h> #include<queue> #include<algorithm> usi ...
- LintCode刷题笔记-- Edit distance
标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...