Java IdentityHashMap isEmpty() 方法



描述

isEmpty() 方法用於測試此標識雜湊對映是否不包含鍵值對映。

宣告

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

public boolean isEmpty()

引數

返回值

如果此標識雜湊對映不包含鍵值對映,則方法呼叫返回“true”。

異常

檢查 Integer,Integer 對 IdentityHashMap 的空性示例

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

package com.tutorialspoint;

import java.util.IdentityHashMap;

public class IdentityHashMapDemo {
   public static void main(String args[]) {
      
      // create identity map
      IdentityHashMap<Integer,Integer> newmap = new IdentityHashMap<>();

      // populate identity 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: {2=2, 3=3, 1=1}
Is map Empty: false
Map elements after clear: {}
Is map Empty: true

檢查 Integer,String 對 IdentityHashMap 的空性示例

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

package com.tutorialspoint;

import java.util.IdentityHashMap;

public class IdentityHashMapDemo {
   public static void main(String args[]) {
      
      // create identity map
      IdentityHashMap<Integer,String> newmap = new IdentityHashMap<>();

      // populate identity 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: {2=point, 3=is best, 1=tutorials}
Is map Empty: false
Map elements after clear: {}
Is map Empty: true

檢查 Integer,Object 對 IdentityHashMap 的空性示例

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

package com.tutorialspoint;

import java.util.IdentityHashMap;

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

      // populate identity 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 ], 3=[ 3, Adam ], 2=[ 2, Robert ]}
Is map Empty: false
Map elements after clear: {}
Is map Empty: true
java_util_identityhashmap.htm
廣告