目录

NavigableMap<K,V> tailMap(K fromKey, boolean inclusive)

描述 (Description)

tailMap(K fromKey,boolean inclusive)方法用于返回此映射部分的视图,其键大于(或等于,如果包含为真)fromKey。 返回的地图由此地图支持,因此返回的地图中的更改将反映在此地图中,反之亦然。

声明 (Declaration)

以下是java.util.TreeMap.tailMap()方法的声明。

public NavigableMap<K,V> tailMap(K fromKey,boolean inclusive)

参数 (Parameters)

  • fromKey - 这是返回映射中键的低端点。

  • inclusive - 如果要将低端点包含在返回的视图中,则为true。

返回值 (Return Value)

方法调用返回此映射部分的视图,其键大于(或等于,如果inclusive为true)fromKey。

异常 (Exception)

  • ClassCastException - 如果fromKey与此映射的比较器不兼容,则抛出此异常。

  • NullPointerException - 如果fromKey为null并且此映射使用自然排序,或者其比较器不允许空键,则抛出此异常。

  • IllegalArgumentException - 如果此映射本身具有受限范围,并且fromKey位于范围的边界之外,则抛出此异常。

例子 (Example)

以下示例显示了java.util.TreeMap.tailMap()的用法

package com.iowiki;
import java.util.*;
public class TreeMapDemo {
   public static void main(String[] args) {
      // creating maps 
      TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();
      SortedMap<Integer, String> treemapincl = new TreeMap<Integer, String>();
      // 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(2,true);
      System.out.println("Tail map values: "+treemapincl);      
   }    
}

让我们编译并运行上面的程序,这将产生以下结果。

Getting tail map
Tail map values: {2=two, 3=three, 5=five, 6=six}
↑回到顶部↑
WIKI教程 @2018