Java中TreeMap的tailMap()方法: TreeMap.tailMap()

作者: Arvin Chen 分类: Java 来源: Break易站(www.breakyizhan.com)

Java中的java.util.TreeMap.tailMap(from_Key)方法用于获取映射的部分或视图,其键大于等于参数中的from_key。在一个map中进行的任何更改都将反映另一个map中的更改。

句法:

Tree_Map.tailMap(from_Key)

参数:该方法从TreeMap中获取Key类型的一个参数from_key,并引用设置为低于其返回其映射的低点的键。

返回值:该方法返回其键大于from_Key的映射部分。

例外:该方法抛出三种类型的异常:

  • ClassCastException:如果方法中提到的参数无法与此映射的键进行比较,则抛出此异常。
  • NullPointerException:如果其中一个参数为null类型且map不接受任何null值,则抛出此异常。
  • IllegalArgumentException:如果提到的参数超出范围,则抛出此异常。

下面的程序说明了java.util.TreeMap.tailMap()方法的工作原理

程序1:

// Java code to illustrate the tailMap() method
import java.util.*;

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

        // Creating an empty TreeMap
        TreeMap<Integer, String> tree_map = new TreeMap<Integer, String>();

        // Mapping string values to int keys
        tree_map.put(10, "Geeks");
        tree_map.put(15, "4");
        tree_map.put(20, "Geeks");
        tree_map.put(25, "Welcomes");
        tree_map.put(30, "You");

        // Displaying the TreeMap
        System.out.println("The original map is: "
                           + tree_map);

        // Displaying the submap
        System.out.println("The tailMap is " + tree_map.tailMap(15));
    }
}
输出:

The original map is: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
The tailMap is {15=4, 20=Geeks, 25=Welcomes, 30=You}

程序2:

// Java code to illustrate the tailMap() method
import java.util.*;

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

        // Creating an empty TreeMap
        TreeMap<String, Integer> tree_map = new TreeMap<String, Integer>();

        // Mapping int values to string keys
        tree_map.put("Geeks", 10);
        tree_map.put("4", 15);
        tree_map.put("Geeks", 20);
        tree_map.put("Welcomes", 25);
        tree_map.put("You", 30);

        // Displaying the TreeMap
        System.out.println("The original map is: "
                           + tree_map);

        // Displaying the tailMap
        System.out.println("The tailMap is " + tree_map.tailMap("Geeks"));
    }
}

输出:

The original map is: {4=15, Geeks=20, Welcomes=25, You=30}
The tailMap is {Geeks=20, Welcomes=25, You=30}

程序3:默认情况下,from_key包含在tail_Map中。如果需要忽略或排除,则可以将另一个参数与from_key一起传递,这是false

// Java code to illustrate the tailMap() method
import java.util.*;

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

        // Creating an empty TreeMap
        TreeMap<String, Integer> tree_map = new TreeMap<String, Integer>();

        // Mapping int values to string keys
        tree_map.put("Geeks", 10);
        tree_map.put("4", 15);
        tree_map.put("Geeks", 20);
        tree_map.put("Welcomes", 25);
        tree_map.put("You", 30);

        // Displaying the TreeMap
        System.out.println("The original map is: "
                           + tree_map);

        // Displaying the tailMap
        System.out.println("The tailMap is " + tree_map.tailMap("Geeks", false));
    }
}

输出:

The original map is: {4=15, Geeks=20, Welcomes=25, You=30}
The tailMap is {Welcomes=25, You=30}
  •   本文标题:Java中TreeMap的tailMap()方法: TreeMap.tailMap() - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/5509.html

    发表笔记

    电子邮件地址不会被公开。 必填项已用*标注

    更多阅读