TreeMap简单simple
TreeMap能够按照主键对里面的数据进行排序,基于上篇文章:java集合类之TreeMap中有关于TreeMap内部实现的详细介绍。本文主要是写了些使用TreeMap的简单demo。
要想实现TreeMap的自动排序功能,要么主键对象实现Comparator接口,要么用Comparable来构造TreeMap。以下则分别对这两种方式创建TreeMap。
1. 继承Comparable
public class Person implements Comparable<Person>{
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((sex == null) ? 0 : sex.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sex == null) {
if (other.sex != null)
return false;
} else if (!sex.equals(other.sex))
return false;
return true;
}
public Person(String name, String sex) {
super();
this.name = name;
this.sex = sex;
}
@Override
public String toString() {
return "Person [name=" + name + ", sex=" + sex + "]";
}
@Override
public int compareTo(Person o) {
Person s = (Person) o;
if(this.name.compareTo(s.getName())>0){
return 1;
}else if(this.name.compareTo(s.getName())<0){
return -1;
}else {
if(this.sex.compareTo(s.getSex())>0){
return 1;
}else if(this.sex.compareTo(s.getSex()) < 0){
return -1;
}else{
return 0;
}
}
}
}
2. 实现Comparator接口
public class MyCompartor implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
if(null == o1){
System.out.println("o1 is null");
return 1;
}
if(null == o2){
System.out.println("o2 is null");
return -1;
}
if(o1.getName().compareTo(o2.getName())>0){
return 1;
}else if(o1.getName().compareTo(o2.getName())<0){
return -1;
}else {
if(o1.getSex().compareTo(o2.getSex())>0){
return 1;
}else if(o1.getSex().compareTo(o2.getSex()) < 0){
return -1;
}else{
return 0;
}
}
}
}
一般主键类尽量使用同一类型,如果使用不同类型则需要分别比较。
public class Student implements Comparable<Object>{
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((sex == null) ? 0 : sex.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sex == null) {
if (other.sex != null)
return false;
} else if (!sex.equals(other.sex))
return false;
return true;
}
public Student(String name, String sex) {
super();
this.name = name;
this.sex = sex;
}
@Override
public String toString() {
return "Student [name=" + name + ", sex=" + sex + "]";
}
@Override
public int compareTo(Object o) {
if(o instanceof Student){
Student s = (Student) o;
if(this.name.compareTo(s.getName())>0){
return 1;
}else if(this.name.compareTo(s.getName())<0){
return -1;
}else {
if(this.sex.compareTo(s.getSex())>0){
return 1;
}else if(this.sex.compareTo(s.getSex()) < 0){
return -1;
}else{
return 0;
}
}
}else if(o instanceof Person){
Person s = (Person) o;
if(this.name.compareTo(s.getName())>0){
return 1;
}else if(this.name.compareTo(s.getName())<0){
return -1;
}else {
if(this.sex.compareTo(s.getSex())>0){
return 1;
}else if(this.sex.compareTo(s.getSex()) < 0){
return -1;
}else{
return 0;
}
}
}else {
return 0;
}
}
}
public class Person implements Comparable<Object>{
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((sex == null) ? 0 : sex.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sex == null) {
if (other.sex != null)
return false;
} else if (!sex.equals(other.sex))
return false;
return true;
}
public Person(String name, String sex) {
super();
this.name = name;
this.sex = sex;
}
@Override
public String toString() {
return "Person [name=" + name + ", sex=" + sex + "]";
}
@Override
public int compareTo(Object o) {
if(o instanceof Student){
Student s = (Student) o;
if(this.name.compareTo(s.getName())>0){
return 1;
}else if(this.name.compareTo(s.getName())<0){
return -1;
}else {
if(this.sex.compareTo(s.getSex())>0){
return 1;
}else if(this.sex.compareTo(s.getSex()) < 0){
return -1;
}else{
return 0;
}
}
}else if(o instanceof Person){
Person s = (Person) o;
if(this.name.compareTo(s.getName())>0){
return 1;
}else if(this.name.compareTo(s.getName())<0){
return -1;
}else {
if(this.sex.compareTo(s.getSex())>0){
return 1;
}else if(this.sex.compareTo(s.getSex()) < 0){
return -1;
}else{
return 0;
}
}
}else {
return 0;
}
}
}
测试:
public class TreeMapExample {
@Test
public void test1(){
TreeMap<Object, Object> treeMap = new TreeMap<Object, Object>();//主键不同类型
Student s1 = new Student("a", "m");
Student s2 = new Student("a", "c");
Person p1 = new Person("d", "w");
Person p2 = new Person("b", "m");
treeMap.put(s1, s1);
treeMap.put(s2, s2);
treeMap.put(p1, p1);
treeMap.put(p2, p2);
for(Map.Entry<Object, Object> entry : treeMap.entrySet()){
System.out.println(entry.getKey().toString());
}
}
@Test
public void test2(){
MyCompartor compartor = new MyCompartor();
TreeMap<Person, Person> treeMap = new TreeMap<Person, Person>(compartor);//主键中放入null
Person s1 = new Person("a", "m");
Person s2 = new Person("a", "c");
Person p1 = new Person("d", "w");
Person p2 = new Person("b", "m");
Person p3 = new Person("b", "c");
treeMap.put(s1, s1);
treeMap.put(s2, s2);
treeMap.put(p1, p1);
treeMap.put(p2, p2);
treeMap.put(null, p3);
System.out.println(treeMap.size());
for(Map.Entry<Person, Person> entry : treeMap.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
TreeMap简单simple的更多相关文章
- 池以及barrier简单
用了下CyclicBarrier,注意线程池中的线程数量设置,还有就是DB连接的时候,需要考虑单个DB能承受的最大连接数目和每个连接上能同时打开的cursor等限制,需要时可以通过jstack查看堆栈 ...
- TreeMap实现原理及源码分析之JDK8
转载 Java 集合系列12之 TreeMap详细介绍(源码解析)和使用示例 一.TreeMap 简单介绍 什么是Map? 在数组中我们通过数组下标来对数组内容进行索引的,而在Map中我们通过对象来对 ...
- Compiler Theory(编译原理)、词法/语法/AST/中间代码优化在Webshell检测上的应用
catalog . 引论 . 构建一个编译器的相关科学 . 程序设计语言基础 . 一个简单的语法制导翻译器 . 简单表达式的翻译器(源代码示例) . 词法分析 . 生成中间代码 . 词法分析器的实现 ...
- [转](四)unity4.6Ugui中文教程文档-------概要-UGUI Visual Components
转自孙广东. 转载请注明出处:http://write.blog.csdn.net/postedit/38922399 更全的内容请看我的游戏蛮牛地址:http://www.unitymanual ...
- OpenBSD为何还在用CVS之感
一个轻松无聊的晚上突然想到一个问题——在当今这个Git大红大紫的时代,OpenBSD为何还在用CVS代码仓库?连他同阵营的FreeBSD都已经改用SVN,宣布逐渐废掉CVS了……问了下google,搜 ...
- 解决SQLite database is locked
前些时候,同事在站点服务端使用SQlite存储一些临时数据,但是在多人并发的时候Sqlite会抛出异常:The database file is locked , database is locked ...
- Microsoft 家族新成员 Datazen 移动BI 介绍
开篇介绍 Microsoft 在上个月即 2015年4月份收购了 Datazen www.datazen.com, Datazen 专注于移动 BI 和数据可视化领域,并且它的基本部署与配置架构都是基 ...
- Sql清理日志文件
场景: 我们导入MR数据时发现磁盘空间不够用了,导致的结果就是我们的程序很可能会抛出异常了,我们需要导入数据的时候进行日志瘦身. 问1:导入数据的时候,瘦身是否会造成数据库的异常? DBA提供解决方案 ...
- SQL Server数据库事务日志存储序列
原文 原文:http://blog.csdn.net/tjvictor/article/details/5251351 如果你的数据库运行在完整或是批量日志恢复模式下,那么你就需要使用作业(job ...
随机推荐
- php精粹-编写高效的php代码 --- php设计模式
1.选择一个最合适的设计模式 没有任何事物是完美的,也没有人说过设计模式一个严格的放之四海而皆准的解决方法.因此你可以改变这些模式,使它们更适合手头的工作.对于某些设计模式而言,他们就是所属程序固有的 ...
- AppDomain(1)-AppDomainSetup
- django之JavaScript的简单学习2
前言:ajax预备知识:json进阶 1.JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON是用字符串来表示Javascript对象: 请大家记住一 ...
- http://www.w3cplus.com/animation/create-animated-text-fills.html
关于svg的资料: http://www.w3cplus.com/animation/create-animated-text-fills.html asp.net中jquery的ajax调用cs文件 ...
- 10 个 jQuery 的无限滚动的插件:
很多社交网站都使用了一些新技术来提高用户体验,而无限滚动的翻页技术就是其中一项,当你页面滑到列表底部时候无需点击就自动加载更多的内容. 下面为你推荐 10 个 jQuery 的无限滚动的插件: 1. ...
- Uva10766 Organising the Organisation
题目链接戳这里 基尔霍夫矩阵裸题.构建基尔霍夫矩阵(度数矩阵-邻接矩阵),求他的任意\(n-1\)阶主子式的绝对值即为答案. 这题开始用java写,结果BigInteger太慢Tle了. 后来用c++ ...
- 搭建mongodb分片
搭建mongodb分片 http://gong1208.iteye.com/blog/1622078 Sharding分片概念 这是一种将海量的数据水平扩展的数据库集群系统,数据分表存储在shardi ...
- python代码优化技巧
转自:http://www.douban.com/group/topic/31478102/ 这个资料库还有些不错的好文章: http://www.ibm.com/developerworks/cn/ ...
- 李洪强iOS开发Swift篇—02_变量和常量
李洪强iOS开发Swift篇—02_变量和常量 一.语言的性能 (1)根据WWDC的展示 在进行复杂对象排序时Objective-C的性能是Python的2.8倍,Swift的性能是Python的3. ...
- 转:三十、Java图形化界面设计——布局管理器之BorderLayout(边界布局)
http://blog.csdn.net/liujun13579/article/details/7772215 边界布局管理器把容器的的布局分为五个位置:CENTER.EAST.WEST.NORTH ...