Java HashMap isEmpty() 方法



描述

isEmpty() 方法用於檢查此對映是否不包含鍵值對映。

宣告

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

public boolean isEmpty()

引數

返回值

如果此對映不包含任何鍵值對映,則方法呼叫返回“true”。

異常

檢查整數,整數對對映是否為空的示例

以下示例演示瞭如何使用 Java HashMap isEmpty() 方法檢查對映是否為空。我們建立了一個 Integer,Integer 對的對映物件。然後添加了一些條目,列印對映。然後使用 isEmpty() 方法檢查對映。使用 clear() 方法清除對映並再次列印,然後再次使用 isEmpty() 方法檢查對映。

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create hash map
      HashMap<Integer,Integer> newmap = new HashMap<>();

      // populate hash map
      newmap.put(1, 1);
      newmap.put(2, 2);
      newmap.put(3, 3); 

      System.out.println("Initial map elements: " + newmap);
      System.out.println("Is map Empty: " + newmap.isEmpty());

      // clear hash map
      newmap.clear();

      System.out.println("Map elements after clear: " + newmap);
      System.out.println("Is map Empty: " + newmap.isEmpty());
   }
}

輸出

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

Initial map elements: {1=1, 2=1, 3=1}
Is map Empty: false
Map elements after clear: {}
Is map Empty: true

檢查整數,字串對對映是否為空的示例

以下示例演示瞭如何使用 Java HashMap isEmpty() 方法檢查對映是否為空。我們建立了一個 Integer,String 對的對映物件。然後添加了一些條目,列印對映。然後使用 isEmpty() 方法檢查對映。使用 clear() 方法清除對映並再次列印,然後再次使用 isEmpty() 方法檢查對映。

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create hash map
      HashMap<Integer,String> newmap = new HashMap<>();

      // populate hash map
      newmap.put(1, "tutorials");
      newmap.put(2, "point");
      newmap.put(3, "is best"); 

      System.out.println("Initial map elements: " + newmap);
      System.out.println("Is map Empty: " + newmap.isEmpty());
      // clear hash map
      newmap.clear();

      System.out.println("Map elements after clear: " + newmap);
      System.out.println("Is map Empty: " + newmap.isEmpty());
   }}

輸出

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

Initial map elements: {1=tutorials, 2=point, 3=is best}
Is map Empty: false
Map elements after clear: {}
Is map Empty: true

檢查整數,學生對對映是否為空的示例

以下示例演示瞭如何使用 Java HashMap isEmpty() 方法檢查對映是否為空。我們建立了一個 Integer,Student 對的對映物件。然後添加了一些條目,列印對映。然後使用 isEmpty() 方法檢查對映。使用 clear() 方法清除對映並再次列印,然後再次使用 isEmpty() 方法檢查對映。

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create hash map
      HashMap<Integer,Student> newmap = new HashMap<>();

      // populate hash map
      newmap.put(1, new Student(1, "Julie"));
      newmap.put(2, new Student(2, "Robert"));
      newmap.put(3, new Student(3, "Adam"));

      System.out.println("Initial map elements: " + newmap);
      System.out.println("Is map Empty: " + newmap.isEmpty());

      // clear hash map
      newmap.clear();

      System.out.println("Map elements after clear: " + newmap);
      System.out.println("Is map Empty: " + newmap.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 + " ]";
   }
}

輸出

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

Initial map elements: {1=[ 1, Julie ], 2=[ 2, Robert ], 3=[ 3, Adam ]}
Is map Empty: false
Map elements after clear: {}
Is map Empty: true
java_util_hashmap.htm
廣告
© . All rights reserved.