import java.util.*; public class TestHashmap{ public static void main(String[] args) { MapTest map=new MapTest(); map.printMap(); } } class MapTest{ HashMap hash=new HashMap(); public void printMap() { hash.put("1","value2"); hash.put("2",null); hash.put("3","value3"); hash.put("4","value4"); String value=(String)hash.get("3"); System.out.println("key 3<===>"+value); Set set=hash.keySet(); System.out.println("set size="+set.size()); Iterator it=set.iterator(); while(it.hasNext()) { String temp2=(String)it.next(); System.out.println("keys contained:"+temp2); } Collection valuesList=hash.values();//no exception will be reported if collection //has null values; instead, the null value will be printed as "null" if iterated. System.out.println("valuesList size="+valuesList.size()); Iterator it2=valuesList.iterator(); while(it2.hasNext()) { String temp2=(String)it2.next(); System.out.println("values contained:"+temp2); } List valueList=new ArrayList(hash.values()); for(int i=0;i<valueList.size();i++) { String temp=(String)valueList.get(i); System.out.println("value"+i+"="+temp); //the sequence is not sure //can not make sure that the values are displayed according to the input order } } }
转载请注明原文地址: https://ibbs.8miu.com/read-200299.html