部分知识点整理 (三)

2014-11-24 11:24:38 · 作者: · 浏览: 14
Map.Entry集合中,并通过其中的getKey()和getValue()放取出键值。

import java.util.*;

class EntrySetDemo

{

public static void main(String[] args)

{

//创建集合,存入元素

Map map = new HashMap();

map.put("01","lisi1");

map.put("02","lisi2");

map.put("03","lisi3");

map.put("04","lisi4");

//获取map集合中的所有键,存入到Set集合中,

Set> entry = map.entrySet();

//通过迭代器取出map中的键值关系,迭代器接收的泛型参数应和Set接收的一致

Iterator> it = entry.iterator();

while (it.hasNext())

{

//将键值关系取出存入Map.Entry这个映射关系集合接口中

Map.Entry me = it.next();

//使用Map.Entry中的方法获取键和值

String key = me.getKey();

String value = me.getValue();

System.out.println(key + " : " + value);

}

}

}