📜  Java TreeMap 特殊方法

📅  最后修改于: 2022-05-13 01:54:37.312000             🧑  作者: Mango

Java TreeMap 特殊方法

Java的TreeMap 与 AbstractMap 类一起用于实现 Map 接口和 NavigableMap。映射根据其键的自然顺序进行排序,或者通过映射创建时提供的 Comparator 进行排序,具体取决于使用的构造函数。由于 NavigableMap 接口和排序数据的实现,TreeMap 提供了一些在任何其他地图实现中不存在的特殊功能。

方法一: firstKey()

它返回地图中当前的第一个(最低)键。

句法:

public K firstKey()

返回值:当前在此映射中的第一个(最低)键。

例子:

Java
//  Java Program to illustrate firstKey() method of TreeMap
 
// Importing input output classes
import java.io.*;
// Importing treeMap class from java.util package
import java.util.TreeMap;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of TreeMap of type Character
        // and String
        TreeMap treeMap
            = new TreeMap<>();
 
        // Inserting elements to the object created above
 
        // Custom entries
        treeMap.put('A', 1);
        treeMap.put('F', 5);
        treeMap.put('M', 2);
        treeMap.put('K', 9);
        treeMap.put('G', 4);
        treeMap.put('J', 7);
 
        // Display all the elements in the object of TreeMap
        System.out.println("Tree Map : " + treeMap);
 
        // Print and display the lowest key
        // using firstkey() method
        System.out.println("Lowest Key is : "
                           + treeMap.firstKey());
    }
}


Java
// Java Program to illustrate lastKey() Method in TreeMap
 
// Importing input output classes
import java.io.*;
// Importing TreeMap class from java.util package
import java.util.TreeMap;
 
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of TreeMap of type Character
        // and Integer
        TreeMap treeMap
            = new TreeMap<>();
 
        // Adding elements to the object created above
        // Custom entries
        treeMap.put('A', 1);
        treeMap.put('F', 5);
        treeMap.put('M', 2);
        treeMap.put('K', 9);
        treeMap.put('G', 4);
        treeMap.put('J', 7);
 
        // Print and display all the elements in the TreeMap
        System.out.println("Tree Map : " + treeMap);
 
        // Print the highest key in the TreeMap
        // using lastKey() method
        System.out.println("Highest Key is : "
                           + treeMap.lastKey());
    }
}


Java
// Java Program to illustrate headMap() method of TreeMap
 
// Importing input output classes
import java.io.*;
// Importing TreeMap class from java.util package
import java.util.TreeMap;
 
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of TreeMap of character and
        // Integer type
        TreeMap treeMap
            = new TreeMap<>();
 
        // Adding elements to the object of TreeMap
        // Custom entries
        treeMap.put('A', 1);
        treeMap.put('F', 5);
        treeMap.put('M', 2);
        treeMap.put('K', 9);
        treeMap.put('G', 4);
        treeMap.put('J', 7);
 
        // Prin and display all elements in the object
        System.out.println("Tree Map : " + treeMap);
 
        // Print elements inclusive of key value passed
        // using headMap() method
        System.out.println(
            "Head Map eclusive of the key value : "
            + treeMap.headMap('G'));
 
        // Similarly to include the value passed
        // We can add a boolean argument as
        System.out.println(
            "Head Map inclusive of the key value : "
            + treeMap.headMap('G', true));
    }
}


Java
// Java Program to illustrate subMap() Method in TreeMap
 
// Importing input output classes
import java.io.*;
// Importing TreeMap class from java.util package
import java.util.TreeMap;
 
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of TreeMap
        // Declaring object of Character and Integer type
        TreeMap treeMap
            = new TreeMap<>();
 
        // Adding elements to the TreeMap object
        // Custom entries
        treeMap.put('A', 1);
        treeMap.put('F', 5);
        treeMap.put('M', 2);
        treeMap.put('K', 9);
        treeMap.put('G', 4);
        treeMap.put('J', 7);
 
        // Print and display all elements of the TreeMap
        System.out.println("Tree Map : " + treeMap);
 
        // Print and display commands illustrating subMap()
        // method
        System.out.println(
            "Submap between the F(inclusive) and K(exclusive) : "
            + treeMap.subMap('F', 'K'));
        System.out.println(
            "Submap between the F(inclusive) and K(inclusive) : "
            + treeMap.subMap('F', true, 'K', true));
        System.out.println(
            "Submap between the F(exclusive) and K(inclusive) : "
            + treeMap.subMap('F', false, 'K', true));
        System.out.println(
            "Submap between the F(exclusive) and K(inclusive) : "
            + treeMap.subMap('F', false, 'K', true));
    }
}


Java
// Java Program to illustrate higherKey() method in TreeMap
 
// Importing input output classes
import java.io.*;
// Importing TreeMap class from java.util package
import java.util.TreeMap;
 
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of TreeaMap object
        TreeMap treeMap
            = new TreeMap<>();
 
        // Adding elements to TreeMap objects
        // Custom entries
        treeMap.put('A', 1);
        treeMap.put('F', 5);
        treeMap.put('M', 2);
        treeMap.put('K', 9);
        treeMap.put('G', 4);
        treeMap.put('J', 7);
 
        // Print and display all TreeMap elements
        System.out.println("Tree Map : " + treeMap);
 
        // Print and display the higher key
        // using higherKey() method
        System.out.println("Higher key for F : "
                           + treeMap.higherKey('F'));
    }
}


Java
// Java Program to illustrate ceilingKey() Method in TreeMap
 
// Importing input output classes
import java.io.*;
// Importing treeMap class from java.util package
import java.util.TreeMap;
 
// main class
class GFG {
 
    // main driver method
    public static void main(String[] args)
    {
        // Creating an object of TreeMap object
        // Declaring object of Character and Integer type
        TreeMap treeMap
            = new TreeMap<>();
 
        // Adding elements to the object created above
        // Custom entries
        treeMap.put('A', 1);
        treeMap.put('F', 5);
        treeMap.put('M', 2);
        treeMap.put('K', 9);
        treeMap.put('G', 4);
        treeMap.put('J', 7);
 
        // print and display all elements of the treeMap
        // object
        System.out.println("Tree Map : " + treeMap);
 
        // Print and display ceiling key among all entries
        // using ceilingKey() method
 
        // Case 1
        System.out.println("Ceiling key for D : "
                           + treeMap.ceilingKey('D'));
 
        // Case 2
        System.out.println("Ceiling key for F : "
                           + treeMap.ceilingKey('F'));
    }
}


Java
// Java Program to illustrate lowerkey() method in TreeMap
 
// Importing input output classes
import java.io.*;
// Importing TreeMap class from java.util package
import java.util.TreeMap;
 
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of TreeMap
        // Declaring object of type - Character and Integer
        TreeMap treeMap
            = new TreeMap<>();
 
        // Adding elements to above object
        // Custom entries
        treeMap.put('A', 1);
        treeMap.put('F', 5);
        treeMap.put('M', 2);
        treeMap.put('K', 9);
        treeMap.put('G', 4);
        treeMap.put('J', 7);
 
        // print and display the TreeMap elements
        System.out.println("Tree Map : " + treeMap);
 
        // Print and display the lower key
        // using lowerKey() method
        System.out.println("Lower key for N : "
                           + treeMap.lowerKey('N'));
        System.out.println("Lower key for M : "
                           + treeMap.lowerKey('M'));
    }
}


Java
// Java Program to illustrate floorKey() method in TreeMap
 
// Importing input output classes
import java.io.*;
// Importing Treemap class from java.util package
import java.util.TreeMap;
 
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of TreeMap of character and
        // Integer type
        TreeMap treeMap
            = new TreeMap<>();
 
        // Adding elements to TreeMap
        // Custom entries
        treeMap.put('A', 1);
        treeMap.put('F', 5);
        treeMap.put('M', 2);
        treeMap.put('K', 9);
        treeMap.put('G', 4);
        treeMap.put('J', 7);
 
        // Print and display the treeMap
        System.out.println("Tree Map : " + treeMap);
 
        // Print and displa ythe flooe key
        // using the floorKey() method
        System.out.println("Floor key for N : "
                           + treeMap.floorKey('N'));
       
        System.out.println("Floor key for M : "
                           + treeMap.floorKey('M'));
    }
}


输出
Tree Map : {A=1, F=5, G=4, J=7, K=9, M=2}
Lowest Key is : A

方法二: lastKey()

Java.util.TreeMap.lastKey() 用于检索地图中存在的最后一个或最高的键。

句法:

tree_map.lastKey();

返回值:该方法返回地图中存在的最后一个键。

异常:如果地图为空,该方法将抛出NoSuchElementException

例子:



Java

// Java Program to illustrate lastKey() Method in TreeMap
 
// Importing input output classes
import java.io.*;
// Importing TreeMap class from java.util package
import java.util.TreeMap;
 
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of TreeMap of type Character
        // and Integer
        TreeMap treeMap
            = new TreeMap<>();
 
        // Adding elements to the object created above
        // Custom entries
        treeMap.put('A', 1);
        treeMap.put('F', 5);
        treeMap.put('M', 2);
        treeMap.put('K', 9);
        treeMap.put('G', 4);
        treeMap.put('J', 7);
 
        // Print and display all the elements in the TreeMap
        System.out.println("Tree Map : " + treeMap);
 
        // Print the highest key in the TreeMap
        // using lastKey() method
        System.out.println("Highest Key is : "
                           + treeMap.lastKey());
    }
}
输出
Tree Map : {A=1, F=5, G=4, J=7, K=9, M=2}
Highest Key is : M

方法三: headMap(Object key_value)

TreeMap 类的Java.util.TreeMap.headMap( key_point ) 方法用于获取严格小于参数 key_value 的所有对或映射部分。提到的参数从新准备的 TreeMap 中排除。由于该集合由地图支持,因此对地图的任何更改都会反映在其他地图中,反之亦然。

句法:

sorted_map = old_treemap.headMap(key_point)

参数:该方法采用TreeMap中所取key类型的一个参数key_point ,并指向要返回键值对的点。

返回值:该方法返回 key 严格小于 key_point 的 key_point 的 treemap 部分。

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

  • ClassCastException:当 key_point 与地图比较器不兼容或不具有可比性时抛出此异常。
  • NullPointerException:当关键点为 Null 时抛出此异常。
  • IllegalArgumentException:当 key_point 超出范围或超出地图范围的限制时抛出此异常。

例子:

Java

// Java Program to illustrate headMap() method of TreeMap
 
// Importing input output classes
import java.io.*;
// Importing TreeMap class from java.util package
import java.util.TreeMap;
 
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of TreeMap of character and
        // Integer type
        TreeMap treeMap
            = new TreeMap<>();
 
        // Adding elements to the object of TreeMap
        // Custom entries
        treeMap.put('A', 1);
        treeMap.put('F', 5);
        treeMap.put('M', 2);
        treeMap.put('K', 9);
        treeMap.put('G', 4);
        treeMap.put('J', 7);
 
        // Prin and display all elements in the object
        System.out.println("Tree Map : " + treeMap);
 
        // Print elements inclusive of key value passed
        // using headMap() method
        System.out.println(
            "Head Map eclusive of the key value : "
            + treeMap.headMap('G'));
 
        // Similarly to include the value passed
        // We can add a boolean argument as
        System.out.println(
            "Head Map inclusive of the key value : "
            + treeMap.headMap('G', true));
    }
}
输出
Tree Map : {A=1, F=5, G=4, J=7, K=9, M=2}
Head Map eclusive of the key value : {A=1, F=5}
Head Map inclusive of the key value : {A=1, F=5, G=4}

方法四: subMap(K startKey, K endKey)



Java的.util.TreeMap.subMap(K startKey,K endKey)在Java方法用来返回通过在参数键的指定范围中定义的地图的一部分或部分。在一张或另一张地图中所做的任何更改都将反映另一张地图中的更改。

句法:

Tree_Map.subMap(K startKey, K endKey)

参数:该方法采用两个 Key 类型的参数:

  • startKey:这是指地图的起点或下端,包括要考虑的点。
  • endKey:这是指地图的端点或更高端,不包括要考虑的点。

返回值:该方法返回另一个地图,其中包含指定范围内的部分或部分地图。

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

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

例子:

Java

// Java Program to illustrate subMap() Method in TreeMap
 
// Importing input output classes
import java.io.*;
// Importing TreeMap class from java.util package
import java.util.TreeMap;
 
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of TreeMap
        // Declaring object of Character and Integer type
        TreeMap treeMap
            = new TreeMap<>();
 
        // Adding elements to the TreeMap object
        // Custom entries
        treeMap.put('A', 1);
        treeMap.put('F', 5);
        treeMap.put('M', 2);
        treeMap.put('K', 9);
        treeMap.put('G', 4);
        treeMap.put('J', 7);
 
        // Print and display all elements of the TreeMap
        System.out.println("Tree Map : " + treeMap);
 
        // Print and display commands illustrating subMap()
        // method
        System.out.println(
            "Submap between the F(inclusive) and K(exclusive) : "
            + treeMap.subMap('F', 'K'));
        System.out.println(
            "Submap between the F(inclusive) and K(inclusive) : "
            + treeMap.subMap('F', true, 'K', true));
        System.out.println(
            "Submap between the F(exclusive) and K(inclusive) : "
            + treeMap.subMap('F', false, 'K', true));
        System.out.println(
            "Submap between the F(exclusive) and K(inclusive) : "
            + treeMap.subMap('F', false, 'K', true));
    }
}
输出
Tree Map : {A=1, F=5, G=4, J=7, K=9, M=2}
Submap between the F(inclusive) and K(exclusive) : {F=5, G=4, J=7}
Submap between the F(inclusive) and K(inclusive) : {F=5, G=4, J=7, K=9}
Submap between the F(exclusive) and K(inclusive) : {G=4, J=7, K=9}
Submap between the F(exclusive) and K(inclusive) : {G=4, J=7, K=9}

方法五: higherKey()

highKey() 方法用于返回严格大于给定键的最小键,如果没有这样的键,则返回 null。

例子:



Java

// Java Program to illustrate higherKey() method in TreeMap
 
// Importing input output classes
import java.io.*;
// Importing TreeMap class from java.util package
import java.util.TreeMap;
 
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of TreeaMap object
        TreeMap treeMap
            = new TreeMap<>();
 
        // Adding elements to TreeMap objects
        // Custom entries
        treeMap.put('A', 1);
        treeMap.put('F', 5);
        treeMap.put('M', 2);
        treeMap.put('K', 9);
        treeMap.put('G', 4);
        treeMap.put('J', 7);
 
        // Print and display all TreeMap elements
        System.out.println("Tree Map : " + treeMap);
 
        // Print and display the higher key
        // using higherKey() method
        System.out.println("Higher key for F : "
                           + treeMap.higherKey('F'));
    }
}
输出
Tree Map : {A=1, F=5, G=4, J=7, K=9, M=2}
Higher key for F : G

方法六: ceilingKey(key)方法

如果没有这样的键,则天花板键()函数返回大于或等于给定键的最小键或 null。

例子:

Java

// Java Program to illustrate ceilingKey() Method in TreeMap
 
// Importing input output classes
import java.io.*;
// Importing treeMap class from java.util package
import java.util.TreeMap;
 
// main class
class GFG {
 
    // main driver method
    public static void main(String[] args)
    {
        // Creating an object of TreeMap object
        // Declaring object of Character and Integer type
        TreeMap treeMap
            = new TreeMap<>();
 
        // Adding elements to the object created above
        // Custom entries
        treeMap.put('A', 1);
        treeMap.put('F', 5);
        treeMap.put('M', 2);
        treeMap.put('K', 9);
        treeMap.put('G', 4);
        treeMap.put('J', 7);
 
        // print and display all elements of the treeMap
        // object
        System.out.println("Tree Map : " + treeMap);
 
        // Print and display ceiling key among all entries
        // using ceilingKey() method
 
        // Case 1
        System.out.println("Ceiling key for D : "
                           + treeMap.ceilingKey('D'));
 
        // Case 2
        System.out.println("Ceiling key for F : "
                           + treeMap.ceilingKey('F'));
    }
}
输出
Tree Map : {A=1, F=5, G=4, J=7, K=9, M=2}
Ceiling key for D : F
Ceiling key for F : F

方法七: lowerKey(key)方法

lowerKey() 方法用于返回严格小于给定键的最大键,作为参数传递。

例子:

Java

// Java Program to illustrate lowerkey() method in TreeMap
 
// Importing input output classes
import java.io.*;
// Importing TreeMap class from java.util package
import java.util.TreeMap;
 
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of TreeMap
        // Declaring object of type - Character and Integer
        TreeMap treeMap
            = new TreeMap<>();
 
        // Adding elements to above object
        // Custom entries
        treeMap.put('A', 1);
        treeMap.put('F', 5);
        treeMap.put('M', 2);
        treeMap.put('K', 9);
        treeMap.put('G', 4);
        treeMap.put('J', 7);
 
        // print and display the TreeMap elements
        System.out.println("Tree Map : " + treeMap);
 
        // Print and display the lower key
        // using lowerKey() method
        System.out.println("Lower key for N : "
                           + treeMap.lowerKey('N'));
        System.out.println("Lower key for M : "
                           + treeMap.lowerKey('M'));
    }
}
输出
Tree Map : {A=1, F=5, G=4, J=7, K=9, M=2}
Lower key for N : M
Lower key for M : K

方法八: floorKey(key)

floorKey() 方法用于从参数中返回小于或等于给定键的最大键。

例子:

Java

// Java Program to illustrate floorKey() method in TreeMap
 
// Importing input output classes
import java.io.*;
// Importing Treemap class from java.util package
import java.util.TreeMap;
 
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating an object of TreeMap of character and
        // Integer type
        TreeMap treeMap
            = new TreeMap<>();
 
        // Adding elements to TreeMap
        // Custom entries
        treeMap.put('A', 1);
        treeMap.put('F', 5);
        treeMap.put('M', 2);
        treeMap.put('K', 9);
        treeMap.put('G', 4);
        treeMap.put('J', 7);
 
        // Print and display the treeMap
        System.out.println("Tree Map : " + treeMap);
 
        // Print and displa ythe flooe key
        // using the floorKey() method
        System.out.println("Floor key for N : "
                           + treeMap.floorKey('N'));
       
        System.out.println("Floor key for M : "
                           + treeMap.floorKey('M'));
    }
}
输出
Tree Map : {A=1, F=5, G=4, J=7, K=9, M=2}
Floor key for N : M
Floor key for M : M