本文為大家分享了使用entrySet方法獲取Map集合中元素的具體代碼,供大家參考,具體內容如下
/*---------------------------------使用entrySet方法取出Map集合中的元素:....該方法是將Map集合中key與value的關系存入到了Set集合中,這個關系的數據類型是Map.Entry....entrySet方法返回值類型的具體寫法為:Set< Map.Entry<KeyType , ValueType> >----------------------------------*/package pack04;import java.util.*;public class MapDemo { public static void main(String[] args) { Map<Integer, String> ma = new HashMap<Integer, String>(); //給集合中存入元素 ma.put(1, "abc01"); //這里將基本數據類型自動裝箱 ma.put(2, "abc02"); ma.put(3, "abc03"); ma.put(4, "abc04"); //將Map集合當中的映射關系取出,存入到Set集合當中 Set< Map.Entry<Integer, String> > entryset = ma.entrySet(); //取迭代器 Iterator< Map.Entry<Integer, String> > it = entryset.iterator(); //獲取Map集合中的元素 while( it.hasNext() ) { Map.Entry<Integer, String> en = it.next(); Integer maKey = en.getKey(); String maValue = en.getValue(); System.out.println( maKey + " : " + maValue ); } }}
注:希望與各位讀者相互交流,共同學習進步。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。
|
新聞熱點
疑難解答
圖片精選