- Java.util 包类
- Java.util - 首页
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util 包其他内容
- Java.util - 接口
- Java.util - 异常
- Java.util - 枚举
- Java.util 有用资源
- Java.util - 有用资源
- Java.util - 讨论
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
广告