java 代码
- public class HashMapDemo{
- public static void main(String [] args){
- HashMap emPhone = new HashMap();
- emPhone.put("Ashish","111-222-3333");
- emPhone.put("Archit","444-555-6666");
- emPhone.put("Prashant","777-888-9999");
- Set keys = emPhone.keySet();//Set类可以获取HashMap对象的键。
- Iterator keyIter = keys.iterator();
- while(keyIter.hasNext()){
- String nextName = (String)keyIter.next();
- String phoneNum = (String)emPhone.get(nextName);
- System.out.println(nextName + ":" + phoneNum);
- }
- String phoneName = (String)emPhone.remove("Ashish");
- System.out.println("Removed Ashish's number :" + phoneName);
- HashMap newEmpPhone = new HashMap();
- newEmpPhone.putAll(emPhone);
- int dirSize = newEmpPhone.size();
- System.out.println("Created new phone directory with " + dirSize + " numbers...");
- phoneName = (String)newEmpPhone.get("Prashant");
- System.out.println("Prashant's number in new director: " + phoneName);
- }
- }