搜档网
当前位置:搜档网 › Map实例讲解

Map实例讲解

内容导读: TreeMap类不仅实现了Map接口,还实现了Map接口的子接口java.util.SortedMap。由TreeMap类实现的Map集合,不允许键对象为null,因为集合中的映射关系是根据键对象按照一定顺序排列的,TreeMap类通


使用TreeMap类
TreeMap 类不仅实现了Map接口,还实现了Map接口的子接口java.util.SortedMap。由TreeMap类实现的Map集合,不允许键对象为 null,因为集合中的映射关系是根据键对象按照一定顺序排列的,TreeMap类通过实现SortedMap接口得到的方法如下所示。
方法名称 功能简介

comparator() 获得对该集合采用的比较器,返回值为Comparator类型,如果未采用任何比较器则返回null

firstKey() 返回在集合中的排序位于第一位的键对象

lastKey() 返回在集合中的排序位于最后一位的键对象

headMap(K toKey) 截取在集合中的排序位于键对象toKey(不包含)之前的所有映射关系,重新生成

一个SortedMap集合并返 回

subMap(K fromKey, K toKey) 截取在集合中的排序位于键对象fromKey(包含)和toKey(不包含)之间的所有映射关系,

重新生成一个SortedMapJ集合并返回

tailMap(K fromKey) 截取在集合中的排序位于键对象fromKey(包含)之后的所有映射关系,重新生成一个SortedMap

集合并返回
表1 TreeMap类通过实现java.util.SortedMap接口得到的方法
在 添加、删除和定位映射关系上,TreeMap类要比HashMap类的性能差一些,但是其中的映射关系具有一定的顺序,如果不需要一个有序的集合,则建议 使用HashMap类;如果需要进行有序的遍历输出,则建议使用TreeMap类,在这种情况下,可以先使用由HashMap类实现的Map集合,在需要 顺序输出时,再利用现有的HashMap类的实例,创建一个具有完全相同映射关系的TreeMap类型的实例,例如下面的例子。
下面的代码首先利 用HashMap类实现一个Map集合,初始化并遍历;然后再利用TreeMap类实现一个Map集合,初始化并遍历,默认按键对象升序排列;最后再利用 TreeMap类实现一个Map集合,初始化为按键对象降序排列,实现方式为将Collections.reverseOrder()作为构造函数 TreeMap(Comparator c)的入口参数,即与默认排序方式相反,关键代码如下:
src\com\mwq\TestCollection.java关键代码:
public static void main(String[] args) {
Person person1 = new Person("马先生", 220181);
Person person2 = new Person("李先生", 220193);
Person person3 = new Person("王小姐", 2

20186);
Map map = new HashMap();
……// 由于篇幅有限,此处省略了向集合中添加映射关系的代码
System.out.println("由HashMap类实现的Map集合,无序:");
for (Iterator it = map.keySet().iterator(); it.hasNext();) {// 遍历集合
Person person = map.get(it.next());
System.out.println(person.getId_card() + " " + person.getName());
}
System.out.println("由TreeMap类实现的Map集合,键对象升序:");
TreeMap treeMap = new TreeMap();
treeMap.putAll(map);
……// 由于篇幅有限,此处省略了遍历集合的代码
System.out.println("由TreeMap类实现的Map集合,键对象降序:");
TreeMap treeMap2 = new TreeMap(
Collections.reverseOrder());// 初始化为反转排序
treeMap2.putAll(map);
……// 由于篇幅有限,此处省略了遍历集合的代码
}
src\com\mwq\TestCollection.java完整代码:
package com.mwq;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class TestCollection {
public static void main(String[] args) {
System.out.println("开始:");
Person person1 = new Person("马先生", 220181);
Person person2 = new Person("李先生", 220193);
Person person3 = new Person("王小姐", 220186);
Map map = new HashMap();
map.put(person1.getId_card(), person1);
map.put(person2.getId_card(), person2);
map.put(person3.getId_card(), person3);
System.out.println("由HashMap类实现的Map集合,无序:");
for (Iterator it = map.keySet().iterator(); it.hasNext();) {// 遍例集合
Person person = map.get(it.next());
System.out.println(person.getId_card() + " " + person.getName());
}
System.out.println("由TreeMap类实现的Map集合,键对象升序:");
TreeMap treeMap = new TreeMap();
treeMap.putAll(map);
for (Iterator it = treeMap.keySet().iterator(); it.hasNext();) {// 遍例集合
Person person = treeMap.get(it.next());
System.out.println(person.getId_card() + " " + person.getName());
}
System.out.println("由TreeMap类实现的Map集合,键对象降序:");
TreeMap treeMap2 = new TreeMap(
Collections.reverseOrder());// 初始化为反转排序
treeMap2.putAll(map);
for (Iterator it = treeMap2.keySet().iterator(); it.hasNext();) {// 遍例集合
Person person = (Person) treeMap2.get(it.next());
System.out.println(person.getId_card() + " " + person.getName());
}
System.out.println("结束!");

相关主题