當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java java.util.WeakHashMap.clear()用法及代碼示例


描述

這個clear()方法用於從此映射中刪除所有映射。

聲明

以下是聲明java.util.WeakHashMap.clear()方法。

public void clear()

參數

NA

返回值

NA

異常

NA

示例

下麵的例子展示了 java.util.WeakHashMap.clear() 方法的用法。

package com.tutorialspoint;

import java.util.Map;
import java.util.WeakHashMap;

public class WeakHashMapDemo {
   public static void main(String[] args) { 
      Map<String, String> weakHashMap = new WeakHashMap<String, String>();

      // put keys and values in the Map      
      weakHashMap.put("1", "first");
      weakHashMap.put("2", "two");
      weakHashMap.put("3", "three");

      // print the contents  
      System.out.println("Contents in the Map");
      System.out.println(weakHashMap);

      // call clear() method
      System.out.println("Clear the Map");
      weakHashMap.clear();

      // again print the contents
      System.out.println("Contents in the Map");
      System.out.println(weakHashMap);
   }    
}

讓我們編譯並運行上麵的程序,這將產生以下結果。

Contents in the Map
{1=first, 2=two, 3=three}
Clear the Map
Contents in the Map
{}

相關用法


注:本文由純淨天空篩選整理自 java.util.WeakHashMap.clear() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。