- 是一个有序的键值对集合
- 基于红黑树理论
- 异步线程
- 有序根据键的顺序排序(升序),如数字或String类型,或者根据创建时放入的比较器(Comparator)进行排序
构造函数
1 默认构造函数
使用默认构造函数构造TreeMap时,使用java的默认的比较器比较Key的大小,从而对TreeMap进行排序。
public TreeMap() {
comparator = null;
}
2 带比较器的构造函数
public TreeMap(Comparator<? super K> comparator) {
this.comparator = comparator;
}
3 带Map的构造函数,Map会成为TreeMap的子集
public TreeMap(Map<? extends K, ? extends V> m) {
comparator = null;
putAll(m);
}
4 带SortedMap的构造函数,SortedMap会成为TreeMap的子集
public TreeMap(SortedMap<K, ? extends V> m) {
comparator = m.comparator();
try {
buildFromSorted(m.size(), m.entrySet().iterator(), null, null);
} catch (java.io.IOException cannotHappen) {
} catch (ClassNotFoundException cannotHappen) {
}
}
常用方法
增
-
V
**put**(K key, V value)
按照一定顺序向Map中增加元素 -
void
**putAll**(Map<? extends K,? extends V> map)
将所有映射复制到此集合中
删除
-
void
**clear**()
从此映射中删除所有映射 -
V
**remove**(Object key)
如果存在,则从此Map中删除此键的值
判断
-
boolean
**isEmpty**
判断此Map是否为空
获取
-
v
**get**
() -
int
**size**()
返回此Map中键 - 值映射的数量-
boolean
**containsValue**(Object value)
如果包含其值,返回true
-
-
boolean
**containsKey**(Object key)
如果包含此键,则返回true
-
V
**get**(Object key)
返回键对应的值,否则返回null
-
Set<K>
**keySet**()
返回一个包含所有键的Set
-
Set<Map.Entry<K,V>>
**entrySet**()
返回一个包含所有映射关系的特殊的类型,Map.Entry
例子
package model.collection_map;
import java.util.Comparator;
import java.util.TreeMap;
//需求:Teacher2类具有姓名、年龄的属性,同时有其归属地(不作为属性)
class TeacherCompare implements Comparator<Teacher2>{
@Override
public int compare(Teacher2 o1, Teacher2 o2) {
int num = o1.getName().compareTo(o2.getName());
if(num==0){
return (new Integer(o1.getAge()).compareTo(new Integer(o1.getAge())));
}return num;
}
}
//将老师存储进TreeMap,不是按照Teacher2自带的比较顺序,而是按照姓名顺序排序
public class TreeMapDemo1 {
public static void main(String[] args) {
TreeMap<Teacher2,String> treeMap1 = new TreeMap<>();
TreeMap<Teacher2,String> treeMap2 = new TreeMap<>(new TeacherCompare());
treeMap1.put(new Teacher2("abc",21),"北京");
treeMap1.put(new Teacher2("bcd",22),"北京");
treeMap1.put(new Teacher2("aca",23),"北京");
treeMap1.put(new Teacher2("bac",24),"北京");
System.out.println("没有改变前:"+treeMap1);
treeMap2.put(new Teacher2("abc",21),"北京");
treeMap2.put(new Teacher2("bcd",22),"北京");
treeMap2.put(new Teacher2("aca",23),"北京");
treeMap2.put(new Teacher2("bac",24),"北京");
System.out.println("改变后:"+treeMap2);
}
}
class Teacher2 implements Comparable<Teacher2> {
protected String name;
protected int age;
public Teacher2(String name, int age) {
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 hashCode() {
return this.name.hashCode() + this.age * 33;
}//重构hashCode以获得独特地址值
@Override
public boolean equals(Object object) {
if (!(object instanceof Teacher2)) {
return false;
}
Teacher2 teacher = (Teacher2) object;
return (teacher.name.equals(this.name)) && (teacher.age == this.age);
}//重构hashCode必须重构equals方法,因为hashCode会调用equals方法
@Override
public String toString() {
return "[name:" + name + ",age:" + age+"]";
}
@Override
public int compareTo(Teacher2 t) {
int num = new Integer(this.age).compareTo(new Integer(t.age));
if (num == 0) {
return this.name.compareTo(t.name);
}
return num;
}//equals方法会调用compareTo方法,为避免输入类型不对而抛异常,需要重构compareTo
}
输出结果
没有改变前:{[name:abc,age:21]=北京, [name:bcd,age:22]=北京, [name:aca,age:23]=北京, [name:bac,age:24]=北京}
改变后:{[name:abc,age:21]=北京, [name:aca,age:23]=北京, [name:bac,age:24]=北京, [name:bcd,age:22]=北京}
网友评论