ITeye博客

Java.在一个循环,同时输出Map的key和value

8850702 · 2016-03-12 18:20 · 移动开发 · Markdown


//在一个循环,同时输出key和value
	public static void main(String[] args) {
		Map<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("a", "1234");
		hashMap.put("m", "oi");
		hashMap.put("p", "看");
		hashMap.put("d", "fuck");
		hashMap.put("e", "2");
		hashMap.put("v", "-_-");//以上,添加元素

		//将Map接口,变为Set接口
		Set<Entry<String, String>> entrySet = hashMap.entrySet();
		//得到Iterator
		Iterator<Entry<String, String>> iterator = entrySet.iterator();
		
		//同时输出key和value
		while (iterator.hasNext()) {
			//通过iterator.next(),得到key和value
			Entry<String, String> next = iterator.next();
			System.out.println(next.getKey() + " -> " + next.getValue());
		}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。