package com.pingan.paic.domain;
import java.util.HashMap;
import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set;public class HashMapTest {
public static void main(String[] args) { Map<Integer, String> map = new HashMap<Integer,String>(); map.put(1, "a"); map.put(2, "b"); map.put(3, "ab"); map.put(4, "ab"); map.put(4, "abc"); System.out.println(map.size()); for (Map.Entry<Integer, String> entry : map.entrySet()) { String key = entry.getKey().toString(); String value = entry.getValue(); System.out.println(key +"....."+value); } // 第一种 Set<Integer> set = map.keySet(); for (Integer integer : set) { String str= map.get(integer); System.out.println(integer+ ":::"+str); } // 第二种 System.out.println("第二种:通过Map.entrySet使用iterator遍历key和value:"); Iterator<Entry<Integer, String>> it = map.entrySet().iterator(); while(it.hasNext()){ Map.Entry<Integer, String> next = it.next(); System.out.println("key="+next.getKey()+" ; and value="+next.getValue()); } // 第三种遍历方式 for (Map.Entry<Integer, String> entry : map.entrySet()) { String key = entry.getKey().toString(); String value = entry.getValue(); System.out.println(key +"....."+value); } }}