從 HashMap 中移除 Java 的值
使用 remove() 方法從 HashMap 中移除值。
首先,建立一個 HashMap 並新增元素 −
HashMap hm = new HashMap();
hm.put("Wallet", new Integer(700));
hm.put("Belt", new Integer(600));
hm.put("Backpack", new Integer(1200));現在,移除一個值,例如鍵為“錢包” −
Object ob = hm.remove("Wallet");以下是從 HashMap 中移除一個值的示例 −
示例
import java.util.*;
public class Demo {
public static void main(String args[]) {
// Create hash map
HashMap hm = new HashMap();
hm.put("Wallet", new Integer(700));
hm.put("Belt", new Integer(600));
hm.put("Backpack", new Integer(1200));
System.out.println("Map = "+hm);
Object ob = hm.remove("Wallet");
System.out.println("Map after removing an element= "+hm);
}
}輸出
Map = {Backpack=1200, Belt=600, Wallet=700}
Map after removing an element= {Backpack=1200, Belt=600}
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP