java中treemap释放_在Java中从TreeMap删除键
使用該remove()方法從TreeMap中刪除鍵。
讓我們首先創建一個TreeMap并添加一些元素-TreeMap?m?=?new?TreeMap();
m.put(1,"India");
m.put(2,"US");
m.put(3,"Australia");
m.put(4,"Netherlands");
m.put(5,"Canada");
現在讓我們刪除鍵。在這里,我們現在要刪除鍵3-m.remove(3)
以下是從TreeMap中刪除鍵的示例-
示例import?java.util.*;
public?class?Demo?{
public?static?void?main(String?args[])?{
TreeMap?m?=?new?TreeMap();
m.put(1,"India");
m.put(2,"US");
m.put(3,"Australia");
m.put(4,"Netherlands");
m.put(5,"Canada");
System.out.println("TreeMap?Elements?=?"+m);
System.out.println("Removing?a?Key?=?"+m.remove(3));
System.out.println("Updated?TreeMap?Elements?=?"+m);
}
}
輸出結果TreeMap?Elements?=?{1=India,?2=US,?3=Australia,?4=Netherlands,?5=Canada}
Removing?a?Key?=?Australia
Updated?TreeMap?Elements?=?{1=India,?2=US,?4=Netherlands,?5=Canada}
總結
以上是生活随笔為你收集整理的java中treemap释放_在Java中从TreeMap删除键的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 知名演员因食道癌去世 从确诊到去世仅1个
- 下一篇: 微信还款信用卡有手续费怎么免