remove(Object key)%uA0方法是用来从这个TreeMap中移除该键的映射。
声明
以下是java.util.TreeMap.remove()方法的声明。
public V remove(Object key)
参数
-
key--这对于映射将被删除的键。
返回值
该方法调用返回与key的前一个值,则返回null,如果没有键的映射关系。
异常
-
ClassCastException--%uA0如果指定键不能与映射中的当前键进行比较,抛出此异常。
-
NullPointerException--%uA0如果指定键为null并且此映射使用自然顺序,或者其比较器不允许使用null键,抛出此异常。
例子
下面的例子显示java.util.TreeMap.remove()方法的使用
package com.yiibai import java.util.* public class TreeMapDemo { public static void main(String[] args) { // creating tree map TreeMap<Integer, String> treemap = new TreeMap<Integer, String>() // 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("Value before modification: "+ treemap) // removing value at key 5 System.out.println("Removed value: "+treemap.remove(5)) System.out.println("Value after modification: "+ treemap) } }
现在编译和运行上面的代码示例,将产生以下结果。
Value before modification: {1=one, 2=two, 3=three, 5=five, 6=six} Removed value: five Value after modification: {1=one, 2=two, 3=three, 6=six}