使用 java.util.TreeMap 类 tailMap headMap fisrtKey lastKey

 

TreeMap 类不仅实现了 Map 接口,还实现了 Map 接口的子接口 java.util.SortedMap。 
TreeMap 类中不允许键对象为 null 或是 基本数据类型,这是因为 TreeMap 中的对象必须是可排序的(即对象需要实现 java.lang.Comparable 接口) 

TreeMap 类通过实现 SortedMap 接口得到的方法如表1所示: 

方法名称返回值类型说明
comparator()Comparator<? super K>获取 TreeMap 实例使用的 Comparator。使用空的构造方法创建的 TreeMap 实例,则返回 null
fisrtKey()K获取第一个(排在最低的)对象的 Key
lastKey()K获取最后个(排在最高的)对象的 Key
headMap(K toKey)SortedMap<K,V>获取一个子集。其所有对象的 key 的值小于 toKey 
subMap(K fromKey, K toKey)SortedMap<K,V>获取一个子集。其所有对象的 key 的值小于 toKey ,大于等于 fromKey 
tailMap(K fromKey)SortedMap<K,V>获取一个子集。其所有对象的 key 的值大于等于 fromKey

表1 TreeMap类通过实现java.util.SortedMap接口得到的方法 


在创建 TreeMap 对象时,如果使用参数为空的构造方法,则根据 Map 对象的 key 进行排序;如果使用参数为 Comparator 的构造方法,则根据 Comparator 进行排序。 

HashMap VS. TreeMap 
在添加、删除和定位映射关系上,TreeMap类要比HashMap类的性能差一些,但是其中的映射关系具有一定的顺序。 
如果不需要一个有序的集合,则建议使用HashMap类;如果需要进行有序的遍历输出,则建议使用TreeMap类。  在这种情况下,可以先使用 HashMap。在需要排序时,利用现有的 HashMap,创建一个 TreeMap 类型的实例(例如下面的例子)。 
 

Java代码  


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<Number, Person> map = new HashMap<Number, Person>();  
        map.put(person1.getId_card(), person1);  
        map.put(person2.getId_card(), person2);  
        map.put(person3.getId_card(), person3);  
          
        // HashMap  
        System.out.println("HashMap,无序:");  
        for (Iterator<Number> it = map.keySet().iterator(); it.hasNext();) {  
            Person person = map.get(it.next());  
            System.out.println(person.getId_card() + " " + person.getName());  
        }  
          
        // TreeMap  
        System.out.println("TreeMap,升序:");  
        TreeMap<Number, Person> treeMap = new TreeMap<Number, Person>();  
        treeMap.putAll(map);  
        for (Iterator<Number> it = treeMap.keySet().iterator(); it.hasNext();) {  
            Person person = treeMap.get(it.next());  
            System.out.println(person.getId_card() + " " + person.getName());  
        }  
          
        System.out.println("TreeMap,降序:");  
        TreeMap<Number, Person> treeMap2 =   
            new TreeMap<Number, Person>(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("结束!");  
    }  
}  

TreeMap 的 tailMap()、headMap()、firstKey() 方法使用

import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Author:  heatdeath
 * Date:    2018/5/13
 * Desc:
 */
public class TreeMapDemo {
    public static void main(String[] args) {
        // creating maps
        TreeMap<Integer, String> treemap = new TreeMap<>();
        SortedMap<Integer, String> treemapincl;

        // populating tree map
        treemap.put(2, "two");
        treemap.put(1, "one");
        treemap.put(3, "three");
        treemap.put(6, "six");
        treemap.put(5, "five");

        System.out.println("Getting tail map");
        treemapincl = treemap.tailMap(3);
        System.out.println("Tail map values: " + treemapincl);

        treemapincl = treemap.headMap(3);
        System.out.println("Head map values: " + treemapincl);

        System.out.println("First key is: " + treemap.firstKey());
    }
}

Getting tail map

Tail map values: {3=three, 5=five, 6=six}

Head map values: {1=one, 2=two}

First key is: 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值