Java Dictionary remove() 方法



描述

Java Dictionary remove(Object key) 方法從該字典中移除值及其對應的鍵。

宣告

以下是 java.util.Dictionary.remove() 方法的宣告

public abstract V remove(Object key)

引數

key - 要移除的鍵。

返回值

此方法返回該鍵對映到的值,如果該鍵沒有對映,則返回 null。

異常

NullPointerException - 如果 key 為 null。

從整數、整數對字典中移除鍵值對映的示例

以下示例演示了 Java Dictionary remove(Object) 方法的用法。我們使用 Integer、Integer 對的 Hashtable 物件建立字典例項。然後向其中添加了一些元素。然後使用 remove(Object) 方法移除一個元素。使用 elements() 方法檢索列舉,然後迭代列舉以列印字典的元素。

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.Dictionary;
import java.util.Hashtable;

public class DictionaryDemo {
   public static void main(String[] args) {

      // create a new hashtable
      Dictionary<Integer, Integer> dictionary = new Hashtable<>();

      // add 3 elements
      dictionary.put(1, 1);
      dictionary.put(2, 2);
      dictionary.put(3, 3);

      // remove one element
      dictionary.remove(2);
	  
      Enumeration<Integer> enumeration = dictionary.elements();

      while(enumeration.hasMoreElements()) {
         System.out.println(enumeration.nextElement());
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

3
1

從整數、字串對字典中移除鍵值對映的示例

以下示例演示了 Java Dictionary remove(Object) 方法的用法。我們使用 Integer、String 對的 Hashtable 物件建立字典例項。然後向其中添加了一些元素。然後使用 remove(Object) 方法移除一個元素。使用 elements() 方法檢索列舉,然後迭代列舉以列印字典的元素。

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.Dictionary;
import java.util.Hashtable;

public class DictionaryDemo {
   public static void main(String[] args) {

      // create a new hashtable
      Dictionary<Integer, String> dictionary = new Hashtable<>();

      // add 2 elements
      dictionary.put(1, "One");
      dictionary.put(2, "Two");
      dictionary.put(3, "Three");

      // remove one element
      dictionary.remove(2);
      Enumeration<String> enumeration = dictionary.elements();

      while(enumeration.hasMoreElements()) {
         System.out.println(enumeration.nextElement());
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Three
One

從整數、物件對字典中移除鍵值對映的示例

以下示例演示了 Java Dictionary remove(Object) 方法的用法。我們使用 Integer、Student 對的 Hashtable 物件建立字典例項。然後向其中添加了一些元素。然後使用 remove(Object) 方法移除一個元素。使用 elements() 方法檢索列舉,然後迭代列舉以列印字典的元素。

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.Dictionary;
import java.util.Hashtable;

public class DictionaryDemo {
   public static void main(String[] args) {

      // create a new hashtable
      Dictionary<Integer, Student> dictionary = new Hashtable<>();

      // add 2 elements
      dictionary.put(1, new Student(1, "Julie"));
      dictionary.put(2, new Student(2, "Robert"));
      dictionary.put(3, new Student(3, "Adam"));

      // remove one element
      dictionary.remove(2);
      Enumeration<Student> enumeration = dictionary.elements();

      while(enumeration.hasMoreElements()) {
         System.out.println(enumeration.nextElement());
      }
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

[ 3, Adam ]
[ 1, Julie ]
java_util_dictionary.htm
廣告

© . All rights reserved.