Java TreeMap的特殊方法
Java中的TreeMap与AbstractMap类一起用于实现Map接口和NavigableMap。地图根据其键的自然顺序进行排序,或者根据地图创建时提供的比较器进行排序,这取决于使用哪个构造函数。由于实现了NavigableMap接口和排序的数据,TreeMap提供了某些特殊的功能,这些功能在其他地图实现中是不存在的。
方法 1: firstKey()
它返回当前地图中的第一个(最低)键。
语法:
public K firstKey()
返回值: 目前在这张地图上的第一个(最低)键。
注意: 如果这个地图是空的,会抛出NoSuchElementException。
示例:
// 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<Character, Integer> 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());
}
}
输出
Tree Map : {A=1, F=5, G=4, J=7, K=9, M=2}
Lowest Key is : A
方法 2: lastKey()
java.util.TreeMap.lastKey()用于检索地图中存在的最后一个或最高一个键。
语法:
tree_map.lastKey();
返回值: 该方法返回地图中存在的最后一个键。
Exception: 如果地图为空,该方法会抛出NoSuchElementException。
示例:
// 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<Character, Integer> 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
方法 3: headMap(Object key_value)
TreeMap类的java.util.TreeMap.headMap( key_point )方法是用来获取所有严格小于参数key_value的对或部分地图的。所提到的参数被排除在新准备的TreeMap中。由于集合是由地图支持的,所以对地图的任何改变都会反映在其他地图上,反之亦然。
语法:
sorted_map = old_treemap.headMap(key_point)
参数: 该方法需要一个参数key_point,即TreeMap中的键的类型,并指的是要返回键值对的那个点。
返回值: 该方法返回treemap中键值严格小于key_point的部分。
Exceptions: 该方法抛出了三种类型的异常。
- ClassCastException。当key_point与地图比较器不兼容或不匹配时,会抛出这个异常。
- NullPointerException。当关键点是空的时候,会抛出这个异常。
- IllegalArgumentException。当key_point超出边界或超出地图范围的限制时,会抛出这个异常。
示例:
// 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<Character, Integer> 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);
// Print 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 exclusive 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 exclusive of the key value : {A=1, F=5}
Head Map inclusive of the key value : {A=1, F=5, G=4}
方法 4: subMap(K startKey, K endKey)
Java中的java.util.TreeMap.subMap( K startKey, K endKey )方法用于返回参数中指定的键值范围所定义的地图的一部分或一部分。在一个或另一个地图中的任何变化都将反映在另一个地图中的变化。
语法:
Tree_Map.subMap(K startKey, K endKey)
参数: 该方法需要两个关键类型的参数。
- startKey。这指的是地图的起点或下端,包括要考虑的点。
- endKey。这指的是地图的端点或较高的端点,不包括要考虑的点。
注意: 如果startKey等于endKey,那么将返回一个Null Map。
返回值: 该方法返回另一张地图,其中包含指定范围内的部分或部分地图。
Exceptions: 该方法抛出了三种类型的异常。
- ClassCastException。如果方法中提到的参数不能与这个地图的键进行比较,就会产生这个异常。
- NullPointerException。如果其中一个参数是空值,而地图不接受任何空值,就会抛出这个异常。
- IllegalArgumentException。如果提到的参数超出了范围,或者低端大于高端,就会抛出这个异常。
示例:
// 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<Character, Integer> 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}
方法 5: higherKey()
higherKey()方法用于返回严格大于给定键的最小键,如果没有这样的键,则为空。
示例:
// 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 TreeMap object
TreeMap<Character, Integer> 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
方法 6: ceilingKey(key) 方法
ceilingKey()函数返回大于或等于给定键的最小键,如果没有这样的键则返回空。
示例:
// 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<Character, Integer> 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
方法 7: lowerKey(key) 方法
lowerKey()方法用于返回严格小于给定键的最大键,作为参数传递。
示例:
// 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<Character, Integer> 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
方法 8: floorKey(key)
floorKey()方法用于返回小于或等于参数中给定键的最大键。
示例:
// 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<Character, Integer> 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 display the floor 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