從 Java 中的 IdentityHashMap 中移除元素


使用 remove() 方法從 IdentityHashMap 中移除元素。

讓我們建立 IdentityHashMap 並新增一些元素

Map<String, Integer> m = new IdentityHashMap<String, Integer>();
m.put("1", 100);
m.put("2", 200);
m.put("3", 300);
m.put("4", 150);
m.put("5", 110);
m.put("6", 50);
m.put("7", 90);
m.put("8", 250);
m.put("9", 350);
m.put("10", 450);

假設你需要從 Map 中移除 2。為此,使用 remove() 方法

m.remove("2");

以下是從 Java IdentityHashMap 中移除元素的示例

範例

 即時演示

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      Map<String, Integer> m = new IdentityHashMap<String, Integer>();
      m.put("1", 100);
      m.put("2", 200);
      m.put("3", 300);
      m.put("4", 150);
      m.put("5", 110);
      m.put("6", 50);
      m.put("7", 90);
      m.put("8", 250);
      m.put("9", 350);
      m.put("10", 450);
      System.out.println("IdentityHashMap elements
"+ m); m.remove("2"); System.out.println("
Updated IdentityHashMap elements
"+ m); } }

輸出如下

IdentityHashMap elements
{2=200, 4=150, 9=350, 7=90, 10=450, 6=50, 5=110, 8=250, 1=100, 3=300}
Updated IdentityHashMap elements
{4=150, 9=350, 7=90, 10=450, 6=50, 5=110, 8=250, 1=100, 3=300}

更新於: 30-Jul-2019

132 次瀏覽

開啟您的 職業生涯

完成課程並獲取認證

開始
廣告