Java TreeMap tailMap() 方法



描述

Java TreeMap tailMap(K fromKey) 方法用于返回此映射中键大于或等于 fromKey 的部分的视图。返回的映射由此映射支持,因此返回的映射中的更改反映在此映射中,反之亦然。

声明

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

public SortedMap<K,V> tailMap(K fromKey)

参数

fromKey − 这是返回映射中键的低端点(包含)。

返回值

方法调用返回此映射中键大于或等于 fromKey 的部分的视图。

异常

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

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

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

Java TreeMap tailMap(K fromKey,boolean inclusive) 方法

描述

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

声明

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

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

参数

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

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

返回值

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

异常

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

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

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

获取 Integer,Integer 对的 TreeMap 的 TailMap 示例

以下示例演示了 Java TreeMap tailMap(K fromKey) 方法的使用,以获取此映射中键大于或等于 fromKey 的部分的视图。我们创建了两个 Integer,Integer 对的 TreeMap 对象。然后将一些条目添加到第一个映射中,并使用 subMap() 从第一个映射中检索和打印子映射。

package com.tutorialspoint;

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

public class TreeMapDemo {
   public static void main(String[] args) {

      // creating maps 
      TreeMap<Integer, Integer> treemap = new TreeMap<>();
      SortedMap<Integer, Integer> treemapincl = new TreeMap<>();

      // populating tree map
      treemap.put(2, 2);
      treemap.put(1, 1);
      treemap.put(3, 3);
      treemap.put(6, 6);
      treemap.put(5, 5);      

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

输出

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

Getting tail map
Tail map values: {3=3, 5=5, 6=6}

获取 Integer,String 对的 TreeMap 的 TailMap 示例

以下示例演示了 Java TreeMap tailMap(K fromKey) 方法的使用,以获取此映射中键大于或等于 fromKey 的部分的视图。我们创建了两个 Integer,String 对的 TreeMap 对象。然后将一些条目添加到第一个映射中,并使用 subMap() 从第一个映射中检索和打印子映射。

package com.tutorialspoint;

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

public class TreeMapDemo {
   public static void main(String[] args) {

      // creating maps 
      TreeMap<Integer, String> treemap = new TreeMap<>();
      SortedMap<Integer, String> treemapincl = new TreeMap<>();

      // 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);      
   }    
}

输出

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

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

获取 Integer,String 对的 TreeMap 的 TailMap 示例

以下示例演示了 Java TreeMap tailMap(K fromKey, boolean inclusive) 方法的使用,以获取此映射中键大于(或等于,如果 inclusive 为 true)fromKey 的部分的视图。我们创建了两个 Integer,String 的 TreeMap 对象。然后将一些条目添加到第一个映射中,并使用 tailMap() 从第一个映射中检索和打印 tailMap。

package com.tutorialspoint;

import java.util.NavigableMap;
import java.util.TreeMap;

public class TreeMapDemo {
   public static void main(String[] args) {

      // creating maps 
      TreeMap<Integer, String> treemap = new TreeMap<>();
      NavigableMap<Integer, String> treemapincl = new TreeMap<>();

      // 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}
java_util_treemap.htm
广告

© . All rights reserved.