天天看点

使用HashMap打印字符串

HashMap<String, String> hashMap=new HashMap<>();//声明hashMap对象

hashMap.put("name", "tom");//hashMap添加字符串

hashMap.put("height", "177");//hashMap添加字符串

hashMap.put("age", "12");//hashMap添加字符串

Iterator<Entry<String, String>> iterator=hashMap.entrySet().iterator();//创建iterator对象

while(iterator.hasNext()){//用while循环,判断是否有下一个

Entry<String, String> entry=iterator.next();//声明entry,并用它来装载字符串

System.out.println("key="+entry.getKey()+"valve="+entry.getValue());//调用getKey()和getValue()打印

}

输出结果:

key=namevalve=tom

key=agevalve=12

key=heightvalve=177