TreeMap和Comparable接口
备注:HashMap线程不安全,效率高,允许key、value为空
HasTable线程安全、效率低、不允许key或value为空
TreeMap在存储时会自动调用comparable方法进行排序,当key为类时可自行调用comparable接口
范例:
package cn.study.lu.four;
import java.util.TreeMap;
public class TestTreeMap {
public static void main(String[] args) {
TreeMap<Integer, String> str1 = new TreeMap<Integer, String>();
str1.put(1001, "aaa");
str1.put(333, "bbb");
str1.put(181, "ccc");
str1.put(9991, "ddd");
for (int k:str1.keySet()) {
System.out.println(k +"---"+str1.get(k));
}
System.out.println(str1);
TreeMap<emp, String> str2 = new TreeMap<emp, String>();
str2.put(new emp(1001, "张三", 10000),"aaa");
str2.put(new emp(1002, "王五", 60000),"bbb");
str2.put(new emp(1003, "里斯", 20000),"ccc");
str2.put(new emp(1004, "赵六", 20000),"ddd");
for(emp i:str2.keySet()) {
System.out.println(i+"---"+str2.get(i));
}
}
}
class emp implements Comparable<emp>{
int id;
String name;
int salary;
public emp(int id, String name, int salary) {
super();
this.id = id;
this.name = name;
this.salary = salary;
}
public String toString() {
return ("id:"+id+",name:"+name+",salary:"+salary);
}
public int compareTo(emp o) {
if (this.salary>o.salary) {
return 1;
}else if(this.salary<o.salary){
return -1;
}else if (this.id>o.id) {
return 1;
}else if(this.id<o.id){
return-1;
}else {
return 0 ;
}
}
}
TreeMap和Comparable接口的更多相关文章
- TreeMap——实现comparable接口并重写CompareTo方法
public class TreeMapTest { public static void main(String[] args) { Map<Student,Integer> stude ...
- Map容器——TreeMap及常用API,Comparator和Comparable接口
TreeMap及常用API ① TreeMap类通过使用红黑树实现Map接口; ② TreeMap提供按排序顺序存储键/值对的有效手段,同时允许快速检索; ③ 不像散列(HashMap), ...
- TreeMap元素必须实现Comparable接口
纠正一下,TreeMap实现一定顺序是通过Comparable接口的,而他实现元素不重复也是完全通过compareTo,而不是hashCode和equals,因为debug不会走到hashCode和e ...
- Java中的TreeMap、Comparable、Comparator
我们知道HashMap的存储位置是按照key这个对象的hashCode来存放的,而TreeMap则是不是按照hashCode来存放,他是按照实现的Comparable接口的compareTo这个方法来 ...
- 第12条:考虑实现Comparable接口
CompareTo方法没有在Object中声明,它是Comparable接口中的唯一的方法,不但允许进行简单的等同性比较,而且允许执行顺序比较.类实现了Comparable接口,就表明它的实例具有内在 ...
- Effective Java 第三版——14.考虑实现Comparable接口
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- 考虑实现Comparable接口
考虑实现Comparable接口 compareTo方法没有在Object中声明.相反,它是Comparable接口中唯一的方法.compareTo方法不但允许进行简单的等同性比较,而且允许执行顺 ...
- Java中Comparator接口和Comparable接口的使用
普通情况下在实现对对象元素的数组或集合进行排序的时候会用到Comparator和Comparable接口,通过在元素所在的类中实现这两个接口中的一个.然后对数组或集合调用Arrays.sort或者Co ...
- Java自定义排序:继承Comparable接口,重写compareTo方法(排序规则)
代码: 1 import java.util.*; 2 3 /** 4 * 学习自定义排序:继承Comparable接口,重写compareTo方法(排序规则). 5 * TreeMap容器的Key是 ...
随机推荐
- vue2.0 之 douban (一)框架搭建 及 整体布局
1.创建豆瓣项目 我们通过官方vue-cli初始化项目 vue init webpack douban 填写项目描述,作者,安装vue-router 初始化后,通过npm install安装依赖 cd ...
- 源码编译apache设置系统启动失败
文章为转载,亲试成功. Apache无法自动启动,1.将apachectl文件拷贝到/etc/rc.d/init.d 中,然后在/etc/rc.d/rc5.d/下加入链接即可.命令如下:cp /usr ...
- ORACLE动态监听
动态监听的原理 pmon在数据库启动到mount或open时,动态从参数文件中读取service_names值.service_names可以为多值(可以有64个,其中包括两个系统的). servi ...
- C# 防火墙操作之特定程序
将特定程序加入防火墙组,与将特定端口加入防火墙流程类似.详情见“C# 防火墙操作之特定端口”.其主要代码为: /// <summary> /// 允许应用程序通过防火墙 /// </ ...
- Ueditor1.4.4 Jsp版本视频上传成功,重新编辑时无法打开、在文本框内无法显示、html源码显示src为空
1. 编辑 ueditor.config.js 第355行 将 whitList 改为 whiteList 2.编辑ueditor.all.js 注释掉7343.7344.7345行代码,即: var ...
- 20160419—JS备忘:服务器回发刷新页面提示重试的解决方案。
有事页面刷新时 提示如下: js使用的是:location.reload()的刷新方式. 使用js重新定向该页面:location.href="a.aspx"; 当使用:self. ...
- Eclipse设置控制台日志输出位置
1.选择服务器配置 2.设置输出文件路径
- http://blog.csdn.net/sdksdk0/article/details/50749326
http://blog.csdn.net/sdksdk0/article/details/50749326
- 每日js练习
第一次玩codewars,选了个最简单的题目 要求是: You have to write a function that describe Leo: if oscar was (integer) 8 ...
- 学习《Oracle PL/SQL 实例讲解 原书第5版》----创建账户
通过readme.pdf创建student账户. 以下用sys账户登录时都是sysdba. 一.PL/SQL 登录oracle. SYS/123 AS SYSDBA 账户名:sys:密码:123:作 ...