java treemap
TreeMap类putAll()方法 (TreeMap Class putAll() method)
putAll() method is available in java.util package.
putAll()方法在java.util包中可用。
putAll() method is used to remove the key-value pairs linked with the given key element (key_ele) that exists in this TreeMap.
putAll()方法用于删除与此TreeMap中存在的给定键元素(key_ele)链接的键值对。
putAll() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
putAll()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
putAll() method may throw an exception at the removing mappings.
putAll()方法可能会在删除映射时引发异常。
- ClassCastException: This exception may throw when the given element is incompatible to compare.ClassCastException :当给定元素不兼容以进行比较时,可能引发此异常。
- NullPointerException: This exception may throw when the given parameter is null exists.NullPointerException :当给定参数为null时,可能引发此异常。
Syntax:
句法:
public Value remove(Object key_ele);
Parameter(s):
参数:
Object key_ele – represents the key element for which key-value pairs is to be removed when exists.
对象key_ele –表示要删除的键值对(如果存在)的键元素。
Return value:
返回值:
The return type of the method is void, it returns the old value linked with the given key element otherwise it returns null.
方法的返回类型为void ,它返回与给定键元素链接的旧值,否则返回null。
Example:
例:
// Java program to demonstrate the example
// of Value remove(Object key_ele) method
// of TreeMap
import java.util.*;
public class RemoveOfTreeMap {
public static void main(String[] args) {
// Instantiates TreeMap
TreeMap < Integer, String > tm = new TreeMap < Integer, String > ();
// By using put() method is
// to put the key-value pairs in
// treemap tm
tm.put(1, "C");
tm.put(4, "C++");
tm.put(3, "Java");
tm.put(2, "Php");
// Display TreeMap tm
System.out.println("tm: " + tm);
// By using remove(2) method is
// to remove the key-value pairs
// exists at the given key element
// "2" when exists
tm.remove(2);
// Display updated TreeMap tm
System.out.println("tm.remove(2): " + tm);
}
}
Output
输出量
tm: {1=C, 2=Php, 3=Java, 4=C++}
tm.remove(2): {1=C, 3=Java, 4=C++}
翻译自: https://www.includehelp.com/java/treemap-remove-method-with-example.aspx
java treemap