1、TreeMap类概述
        键是红黑树结构,可以保证键的排序和唯一性
2、TreeMap案例
        TreeMap<String,String>
        TreeMap<Student,String>
 
例子1:
package treemapdemos;
import java.util.Set;
import java.util.TreeMap;
/**
* Created by gao on 15-12-22.
*/
/*
* TreeMap:是基于红黑树的Map接口的实现。
* HashMap<String,String>
* 键:String
* 值:String
*/
public class TreeMapDemo01 {
public static void main(String[] args) {
// 创建集合对象
TreeMap<String, String> tm = new TreeMap<String, String>();
// 创建元素并添加元素
tm.put("hello", "你好");
tm.put("world", "世界");
tm.put("java", "爪哇");
tm.put("world", "世界2");
tm.put("javaee", "爪哇EE");
// 遍历集合
Set<String> set = tm.keySet();
for (String key : set) {
String value = tm.get(key);
System.out.println(key + "---" + value);
}
}
}

输出结果:(唯一和自然排序)

hello---你好
java---爪哇
javaee---爪哇EE
world---世界2
 
例子2:

package treemapdemos;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;
/**
* Created by gao on 15-12-22.
*/
/*
* TreeMap<Student,String>
* 键:Student
* 值:String
*/
public class TreeMapDemo02 {
public static void main(String[] args) {
// 创建集合对象
TreeMap<Student, String> tm = new TreeMap<Student, String>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getAge() - s2.getAge();
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
return num2;
}
});
// 创建学生对象
Student s1 = new Student("潘安", 30);
Student s2 = new Student("柳下惠", 35);
Student s3 = new Student("唐伯虎", 33);
Student s4 = new Student("燕青", 32);
Student s5 = new Student("唐伯虎", 33);
// 存储元素
tm.put(s1, "宋朝");
tm.put(s2, "元朝");
tm.put(s3, "明朝");
tm.put(s4, "清朝");
tm.put(s5, "汉朝");
// 遍历
Set<Student> set = tm.keySet();
for (Student key : set) {
String value = tm.get(key);
System.out.println(key.getName() + "---" + key.getAge() + "---" + value);
}
}
}
输出结果:(唯一和排序)
潘安---30---宋朝
燕青---32---清朝
唐伯虎---33---汉朝
柳下惠---35---元朝
 
 
 
 
 
 
 

Java API —— TreeMap类的更多相关文章

  1. Java API 常用类(一)

    Java API 常用类 super类详解 "super"关键字代表父类对象.通过使用super关键字,可以访问父类的属性或方法,也可以在子类构造方法中调用父类的构造方法,以便初始 ...

  2. Java:TreeMap类小记

    Java:TreeMap类小记 对 Java 中的 TreeMap类,做一个微不足道的小小小小记 概述 前言:之前已经小小分析了一波 HashMap类.HashTable类.ConcurrentHas ...

  3. es2.4.6 java api 工具类

    网上找了很久没找到2.4.X 想要的java api 工具 自己写了一个,分享一下 导入所需的jar <!-- ElasticSearch begin --> <dependency ...

  4. Java API —— TreeSet类

    1.TreeSet类    1)TreeSet类概述         使用元素的自然顺序对元素进行排序         或者根据创建 set 时提供的 Comparator 进行排序          ...

  5. Java API —— DateFormat类

    1.DateFormat类概述         DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间. 是抽象类,所以使用其子类SimpleDateForm ...

  6. Java API ——StringBuffer类

    1.StringBuffer类概述 1)我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间.而StringBuffer就可以解决这个问题 2)线程安全的可变字 ...

  7. JDK1.8源码(十一)——java.util.TreeMap类

    在前面几篇博客分别介绍了这样几种集合,基于数组实现的ArrayList 类,基于链表实现的LinkedList 类,基于散列表实现的HashMap 类,本篇博客我们来介绍另一种数据类型,基于树实现的T ...

  8. Java—API/Obiect类的equals toString方法/String类/StringBuffer类/正则表达式

    API  Java 的API(API: Application(应用) Programming(程序) Interface(接口)) 就是JDK中提供给我们使用的类,这些类将底层的代码实现封装了起来 ...

  9. Java API —— File类

    1.File类的概述         文件和目录路径名的抽象表示形式,创建File对象后,仅仅是一个路径的表示,不代码具体的事物一定是存在的. 2.构造方法         · public File ...

随机推荐

  1. 【转载】Powershell设置世纪互联Office365嵌套组发送权限

    Start-Transcript ".\Set-GroupSendPermisionLog.txt" -Force function Get-DLMemberRecurse { $ ...

  2. 表达式语言之EL表达式

    1.EL的用法EL的起源:起源于JSTL.EL运算符: 算术型:+.-.*./.div.%.mod.其中/和div都表示求除.%和mod表示求余数. 逻辑型:and或&&.or或||. ...

  3. Linux磁盘空间爆满,MySQL无法启动

    OS: Cent OS 6.3 DB: 5.5.14 看到一个帖子,在服务器上安装了oracle和mysql数据库,mysql数据库忘记开启innodb_file_per_table,导致插入测试数据 ...

  4. java 格式化日期(DateFormat)

    import java.text.DateFormat; import java.util.Date; /** * 格式化时间类 DateFormat.FULL = 0 * DateFormat.DE ...

  5. [ios]ios的延迟执行方法

    1.最直接的方法performSelector:withObject:afterDelay: 这种方法的缺点:每次要为延时写一个方法   2.使用类别,用BOLCK执行 [代码]c#/cpp/oc代码 ...

  6. 使用parseJSON代替eval

    有些程序员如果没有很好的在javascript中解析json数据,往往会直接eval把json转成js对象,这时候如果json的数据中包含了被注入的恶意数据,则可能导致代码注入的问题. 正确的做法是分 ...

  7. Text selection in div(contenteditable) when double click

    背景: 在最近项目中,碰到一个问题:有一个可编辑的div需要双击时可编辑,blur或者回车时将编辑结果保存.你可能注意到双击时,文字会被选中,可编辑区域不会focus到光标位置.考虑到兼容性问题,写了 ...

  8. How does java technology relate to cloud computing?

    Java Paas shootout   (@IBM developer) Cloud computing is always a hot topic around IT field today.Ho ...

  9. UVA 10954 Add All 哈夫曼编码

    题目链接: 题目 Add All Time Limit:3000MS Memory Limit:0KB 问题描述 Yup!! The problem name reflects your task; ...

  10. win8双屏敲代码

    23寸,AOC冠杰("AOC I2369V 23英寸LED背光超窄边框IPS广视角液晶显示器(银色)") 某东,920买入.