PackageCn.jbit.map;ImportJava.util.HashMap;ImportJava.util.Map;/*** Test multiple methods of HashMap. */ Public classTestMap { Public Static voidMain (string[] args) {//1. Use HashMap to store key-value pairs for multiple sets of national abbreviations and Chinese namesMap countries =NewHashMap (); Countries.put ("CN", "People's Republic of China"); Countries.put ("RU", "Russian Federation"); Countries.put ("FR", "French Republic"); Countries.put ("US", "United States of America"); //2. Display the Chinese full name of the corresponding country "CN"String country = (string) countries.get ("CN"); System.out.println ("CN corresponds to the country:" +country); //3. Display the number of elements in the collectionSYSTEM.OUT.PRINTLN ("Map has" +countries.size () + "group data"); /*4. Two times to determine if the "FR" key exists in the map*/System.out.println ("Does the map contain a FR key?" +Countries.containskey ("FR")); Countries.remove ("FR"); System.out.println ("Does the map contain a FR key?" +Countries.containskey ("FR")); /*5. Display key set, value set and key value pair set respectively*/System.out.println (Countries.keyset ()); System.out.println (Countries.values ()); SYSTEM.OUT.PRINTLN (countries); /*3. Empty the HashMap and Judge*/countries.clear (); if(Countries.isempty ()) System.out.println ("The data in the map has been emptied!" "); }}
Multiple methods of 24.HashMap