Java中怎樣遍歷Map的所有的元素

2021-08-25 09:41:19 字數 1040 閱讀 9285

jdk1.4中

map map = new hashmap();

iterator it = map.entryset().iterator();

while (it.hasnext())

jdk1.5中 應用新特性for-each迴圈

map m = new hashmap();

for(object o : map.keyset())返回的 set 中的每個元素都是乙個 map.entry 型別。

private hashtable emails = new hashtable(); 另外 我們可以先把hashmap 轉為集合collection,再迭代輸出,不過得到的物件

//方法一: 用entryset()

iterator it = emails.entryset().iterator();

while(it.hasnext())

// 方法二:jdk1.5支援,用entryset()和for-each迴圈()

for (map.entry m : emails.entryset())

// 方法三:用keyset()

iterator it = emails.keyset().iterator();

while (it.hasnext())

// 方法五:jdk1.5支援,用keyset()和for-each迴圈

for(object m: emails.keyset())

map aa = new hashmap();

aa.put("tmp1", new object());//追加替換用同樣的函式.

aa.remove("temp1");//刪除

for(iterator i = aa.values().iterator(); i.hasne();)

來個完整的,包含treeset的元素內部排序的

public static void main(string args) {

arraylist list = new arraylist();

hashmap

Java中怎樣遍歷Map的所有的元素

jdk1.4中 map map new hashmap iterator it map.entryset iterator while it.hasnext 返回的 set 中的每個元素都是乙個 map.entry 型別。private hashtable emails new hashtable ...

JAVA遍歷Map所有元素

jdk1.5 map map new hashmap for object obj map.keyset jdk1.4 map map new hashmap iterator iter map.entryset iterator while iter.hasnext 第一種 map map new...

java中Map的遍歷

map遍歷的常用方法 mapmap new hashmap map.put 0,zero map.put 1,one map.put 2,two 方法一 最常用的 獲取key值 collectionk map.keyset iteratoritk k.iterator system.out.prin...