Java Dictionary isEmpty() 方法



描述

Java Dictionary.isEmpty() 方法檢查此字典是否沒有鍵值對。

宣告

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

public abstract boolean isEmpty()

引數

返回值

如果此字典沒有鍵值對,則此方法返回 true;否則返回 false。

異常

檢查整數、整數對字典是否為空的示例

以下示例演示了 Java Dictionary isEmpty() 方法的用法。我們使用 Integer、Integer 對的 Hashtable 物件建立一個字典例項。我們使用 isEmpty() 方法檢查字典的狀態並列印結果。然後我們向其中添加了一些元素,並使用 isEmpty() 方法檢查字典的狀態並列印結果。

package com.tutorialspoint;

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<>();
      System.out.println(dictionary.isEmpty());

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

      System.out.println(dictionary.isEmpty());
   }
}

輸出

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

true
false

檢查整數、字串對字典是否為空的示例

以下示例演示了 Java Dictionary isEmpty() 方法的用法。我們使用 Integer、String 對的 Hashtable 物件建立一個字典例項。我們使用 isEmpty() 方法檢查字典的狀態並列印結果。然後我們向其中添加了一些元素,並使用 isEmpty() 方法檢查字典的狀態並列印結果。

package com.tutorialspoint;

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<>();
      System.out.println(dictionary.isEmpty());

      // add 2 elements
      dictionary.put(1, "One");
      dictionary.put(2, "Two");
      System.out.println(dictionary.isEmpty());
   }
}

輸出

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

true
false

檢查整數、物件對字典是否為空的示例

以下示例演示了 Java Dictionary isEmpty() 方法的用法。我們使用 Integer、Student 對的 Hashtable 物件建立一個字典例項。我們使用 isEmpty() 方法檢查字典的狀態並列印結果。然後我們向其中添加了一些元素,並使用 isEmpty() 方法檢查字典的狀態並列印結果。

package com.tutorialspoint;

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<>();
      System.out.println(dictionary.isEmpty());

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

      System.out.println(dictionary.isEmpty());
   }
}
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 + " ]";
   }
}

輸出

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

true
false
java_util_dictionary.htm
廣告
© . All rights reserved.