(1)TreeMap类通过使用红黑树实现Map接口
(2)TreeMap提供按排序顺序存储键值对的有效手段,同时允许快速检索
(3)不同于散列映射,树映射保证它的元素按键的自然顺序升序排列
(4)TreeMap构造方法:
TreeMap()
TreeMap(Comparator comp)
TreeMap(Map m)
TreeMap(SortedMap sm)
(5)TreeMap实现SortedMap并且扩展AbstractMap,它本身并没有定义其他方法
JDK中对treemap的定义如下:
public class TreeMap<K,V>extends AbstractMap<K,V>implements SortedMap<K,V>, Cloneable, Serializable
SortedMap 接口的基于红黑树的实现。此类保证了映射按照升序顺序排列关键字,根据使用的构造方法不同,可能会按照键的类的自然顺序 进行排序(参见 Comparable),或者按照创建时所提供的比较器进行排序。
treemap构造方法
TreeMap的key存储引用类型数据,需要满足一定条件:
要么引用类型实现Comparable接口,要么在构建TreeMap对象时,为该TreeMap容器提供实现Comparator接口的比较器对象
package com.iotek.treemap; import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap; public class TreeMapDemo1 { /**
* @param args
*/
public static void main(String[] args) {
TreeMap<String, String> tmap = new TreeMap<String, String>();
tmap.put("jack", "zhangsan");
tmap.put("mary", "xiaohong");
tmap.put("rose", "xiaozhang");
tmap.put("free", "xiaoming");
tmap.put("rose", "chenming");
System.out.println(tmap);
// 输出结果是,第三个put进去的键值对被重名的后添加的键对应的键值对替换了,键是唯一的
Set<Entry<String, String>> entrys = tmap.entrySet();
/*
* 用entrySet()方法返回map容器中的entry对象,然后用foreach来遍历键值对,
* 用一个Entry类型的引用变量entry,调用entry的getKey 和getValue方法
*/
for (Entry<String, String> entry : entrys) {
System.out.println(entry.getKey() + "--" + entry.getValue());
} } }
package com.iotek.treemap; import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap; public class TreeMapDemo2 { /**
* @param args
*/
public static void main(String[] args) {
TreeMap<Person, String> pdata = new TreeMap<Person, String>();
// 构造一个新的空映射,该映射根据给定的比较器进行排序
pdata.put(new Person("zhangsan", 30), "张三");
pdata.put(new Person("lisi", 31), "李四");
pdata.put(new Person("rose", 32), "玫瑰");
pdata.put(new Person("zhangsan", 33), "张三");
Set<Entry<Person, String>> entrys = pdata.entrySet();
for (Entry<Person, String> entry : entrys) {
System.out.println(entry.getKey() + "--" + entry.getValue());
}
// 如果Person类不实现Comparable接口,会报错。因为默认对象作为键名时,不会实现Comparable接口
} } class Person implements Comparable<Person> {
private String name;
private int age; public Person(String name, int age) {
super();
this.name = name;
this.age = age;
} 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;
} @Override
public int compareTo(Person o) { //按age的自然顺序进行升序排序
if (this.age - o.getAge() > 0) { // 如果当前Person对象的age比形参的Person对象的age大,则返回1
// 如果o1的age大于o2的age,就放在二叉树节点的右边
return 1;
} else if (this.age - o.getAge() < 0) {
return -1;
}
return 0;
} @Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
} }
结果:按照年龄升序排序:
按照名字和age排序:
package com.iotek.treemap; import java.util.Comparator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap; public class TreeMapDemo3 { public static void main(String[] args) {
// TreeMap的构造方法:TreeMap(Comparator<? super K> c)/
// 构造一个新的空映射,该映射根据给定的比较器进行排序
TreeMap<Person1, String> pdata = new TreeMap<Person1, String>(
new Comparator<Person1>() {
@Override
public int compare(Person1 o1, Person1 o2) {
if (o1.getName().compareTo(o2.getName()) > 0) {
/* o1.getName()得到的是字符串,调用其 compareTo()方法,该方法按字符串的自然顺序进行排序(字符串比较大小) */
return 1;
} else if (o1.getName().compareTo(o2.getName()) < 0) {
return -1;
} else { // 当名字相同时,再按照年龄来比较
return o1.getAge() - o2.getAge();
}
} }); pdata.put(new Person1("zhangsan", 30), "张三");
pdata.put(new Person1("lisi", 31), "李四");
pdata.put(new Person1("rose", 32), "玫瑰");
pdata.put(new Person1("zhangsan", 33), "张三");
Set<Entry<Person1, String>> entrys = pdata.entrySet();
for (Entry<Person1, String> entry : entrys) {
System.out.println(entry.getKey() + "--" + entry.getValue());
} } } /*
* 按字符串的自然顺序进行排序(字符串比较大小):String类的方法 int
* compareTo(String anotherString) 按字典顺序比较两个字符串。
* anotherString - 要比较的 String。 返回:如果参数字符串等于此字符串, 则返回 0
* 值; 如果按字典顺序此字符串小于字符串参数,则返回一个小于 0 的值;如果按字典顺序此字符串
* 大于字符串参数, 则返回一个大于 0 的值。
*/
/* o1.getName()得到的是字符串,调用其 compareTo()方法 */ class Person1 {
private String name;
private int age; public Person1(String name, int age) {
super();
this.name = name;
this.age = age;
} 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;
} @Override
public String toString() {
return "Person1 [name=" + name + ", age=" + age + "]";
} }
结果如下: