“黑马程序员” 集合框架

 

 

android培训java培训期待与您交流!!!

本片主要介绍集合体系,以及各种集合容器的特点。

 



 

 

 

下面的代码演示遍历map集合的三种方式

 

 

package com.DoMap;
import java.util.*;
import java.util.Map.Entry;
public class IterateMap {

	/**
	 * 遍历Map集合的三种方式
	 */
	public static void main(String[] args) {
		Map<String, Student> map=new HashMap<String, Student>();
		map.put("s1", new Student(21));
		map.put("s2", new Student(22));
		map.put("s3", new Student(23));
		IterateMothod1(map);
		IterateMothod2(map);
		IterateMothod3(map);
	}
/**
 * 通过值的集合遍历Map
 * @param map
 */
	public  static void IterateMothod1(Map<String, Student> map) {
		Collection<Student> cls=map.values();
		for(Student s:cls){
			System.out.println(s.getAge());
		}
	}
	/**
	 * 通过键的集合遍历Map
	 * @param map
	 */
	public  static void IterateMothod2(Map<String, Student> map) {
		Set<String> ks=map.keySet();
		Iterator<String> it=ks.iterator();
		while(it.hasNext()){
			//获取键
			String key=it.next();
			//通过键获取值
			Student stu=map.get(key);
			System.out.println(stu.getAge());
		}
	}
	/**
	 * 通过entry(键值对)集合遍历Map
	 * @param map
	 */
	public  static void IterateMothod3(Map<String, Student> map) {
		Set<Entry<String, Student>> es=map.entrySet();
		for(Entry<String, Student> entry:es){
			System.out.println(entry.getKey());//获取键的值
			System.out.println(entry.getValue().getAge());
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值