遍历集合map集合的几种方法

public static void main(String[] args) {
	Map<String ,Object > map=new HashMap<String, Object>();
	map.put("1","张三");
	map.put("2","lisi");
	map.put("3","wangwu");
	map.put("4","maliu");
	map.put("5","hanmeiemi");
	map.put("6","kaai");
	
	//打印对象
	System.out.println(map.entrySet());
	//打印key对应的值
	for(Object  entry:map.values()){
		System.out.println(entry);
	}
	//打印key值
	for(String   entry:map.keySet()){
		System.out.println(entry);
	}
	//增强型for循环key---value
	for(Map.Entry<String , Object >  entry:map.entrySet()){
		System.out.println(entry.getKey()+"--------"+entry.getValue());
		System.out.println(entry.getValue());
	}
	//使用迭代器遍历map集合
	Iterator<Map.Entry<String, Object >> iterator= map.entrySet().iterator();
	while (iterator.hasNext()) {
		//打印的是所有的对象
		System.out.println(iterator.next());
		//将对象放在一个集合中
		Entry<String ,Object> entry=iterator.next();
		System.out.println(entry.getKey());
		System.out.println(entry.getValue());
	}
	
	
}

猜你喜欢

转载自blog.csdn.net/null111666/article/details/87930201