码迷,mamicode.com
首页 > 编程语言 > 详细

java Map扩展知识练习

时间:2016-05-12 15:54:44      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

/*
map扩展知识。
map集合被使用是因为映射关系。
编程实现以下数据:
"yureban"  -> "01" "zhangsan"
"yureban"  -> "02" "lisi"

"jiuyeban" -> "01" "wangwu"
"jiuyeban" -> "02" "zhaoliu"
注意一个键对应一个值,所以根据上面的例子,应该想到这个“值”本身也要是一个集合来存储具有映射关系的多对值。
*/
import java.util.*;
class MapTest4 
{
	public static void main(String[] args) 
	{
		HashMap<String,HashMap<String,String>> hm=new HashMap<String,HashMap<String,String>>();

		HashMap<String,String> yure=new HashMap<String,String>();
		HashMap<String,String> jiuye=new HashMap<String,String>();

		yure.put("01","yure_stu_001");
		yure.put("02","yure_stu_002");

		jiuye.put("01","jiuye_stu_01");
		jiuye.put("02","jiuye_stu_02");

		hm.put("yure",yure);
		hm.put("jiuye",jiuye);

		//print_1(hm,"yure");
		//sop("--------");
		//print_2(hm,"jiuye");

		print_All1(hm);

	}


	public static void print_All1(HashMap<String,HashMap<String,String>> hm)
	{
		Set<Map.Entry<String,HashMap<String,String>>> set=hm.entrySet();

		Iterator<Map.Entry<String,HashMap<String,String>>> it=set.iterator();
		while(it.hasNext())
		{
			Map.Entry<String,HashMap<String,String>> me=it.next();
			String key=me.getKey();
			HashMap<String,String> hash=me.getValue();
			Set<Map.Entry<String,String>> s=hash.entrySet();
			Iterator<Map.Entry<String,String>> it1=s.iterator();
			while(it1.hasNext())
			{
				Map.Entry<String,String> me1=it1.next();
				String sub_key=me1.getKey();
				String sub_value=me1.getValue();
				sop(key+"....."+sub_key+"......"+sub_value);
			}
		}
	}


	public static void print_All2(HashMap<String,HashMap<String,String>> hm)
	{
		Set<String> set=hm.keySet();  //获取教室名称

		Iterator<String> it=set.iterator();
		while(it.hasNext())
		{
			HashMap<String,String> hash=hm.get(it.next());
			Set<Map.Entry<String,String>> s=hash.entrySet();
			Iterator<Map.Entry<String,String>> it1=s.iterator();
			while(it1.hasNext())
			{
				Map.Entry<String,String> me=it1.next();
				String key=me.getKey();
				String value=me.getValue();
				sop("print--->"+key+"..."+value);
			}
		}
	}



	public static void print_1(HashMap<String,HashMap<String,String>> hm,String room)
	{
		HashMap<String,String> hash=hm.get(room);
		Set<String> set=hash.keySet();

		Iterator<String> it=set.iterator();
		while(it.hasNext())
		{
			String key=it.next();
			String name=hash.get(key);
			sop(key+"..."+name);
		}

	}

	public static void print_2(HashMap<String,HashMap<String,String>> hm,String room)
	{
		HashMap<String,String> hash=hm.get(room);

		Set<Map.Entry<String,String>> set=hash.entrySet();
		Iterator<Map.Entry<String,String>> it=set.iterator();

		while(it.hasNext())
		{
			Map.Entry<String,String> me=it.next();
			String key=me.getKey();
			String value=me.getValue();
			sop(key+"..."+value);
		}
	}

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}


/*
map扩展知识(续)。

编程实现存储以下结构的数据:

"yureban"  Student("001","zhangsan")
"yureban"  Student("002","lisi")

"jiuyeban" Student("001","wangwu")
"jiuyeban" Student("002","zaholiu")
 
 
 解题分析:
 不能将班级名称作为键,将学生对象作为值存入map中,因为键时重复的。
 但是可以构造这样的数据结构,即班级名称对应一个集合,这个集合存储多个学生的信息。
*/
import java.util.*;
class Student
{
	private String no;
	private String name;
	Student(String no,String name)
	{
		this.no=no;
		this.name=name;
	}

	public String getNo()
	{
		return no;
	}

	public String getName()
	{
		return name;
	}
	
	public boolean equals(Object obj)
	{
		if(!(obj instanceof Student))
			throw new RuntimeException("不是学生对象");
		Student s=(Student)obj;
		boolean flag=this.no.equals(s.no) && this.name.equals(s.name);
		//MapTest5.sop(this.no+"..."+s.no);
		//MapTest5.sop(flag);
		return flag;
	}

}


class MapTest5 
{
	public static void main(String[] args) 
	{
		HashMap<String,ArrayList<Student>> hash=new HashMap<String,ArrayList<Student>>();
		

		ArrayList<Student> yure=new ArrayList<Student>();
		yure.add(new Student("001","zhangsan001"));
		yure.add(new Student("002","zhangsan002"));
		yure.add(new Student("005","zhangsan005"));
		yure.add(new Student("004","zhangsan004"));
		yure.add(new Student("003","zhangsan003"));
		yure.add(new Student("003","zhangsan003"));

		yure=quchong(yure);
		hash.put("yure",yure);

		ArrayList<Student> jiuye=new ArrayList<Student>();
		jiuye.add(new Student("001","lisi001"));
		jiuye.add(new Student("002","lisi002"));
		jiuye.add(new Student("005","lisi005"));
		jiuye.add(new Student("004","lisi004"));
		jiuye.add(new Student("003","lisi003"));
		jiuye.add(new Student("004","lisi004"));
		jiuye=quchong(jiuye);
		hash.put("jiuye",jiuye);
		//sop(hash);
		print(hash);




		/*
		Iterator it=yure.iterator();
		while(it.hasNext())
		{
			Student s=(Student)it.next();
			sop(s.getNo()+"..."+s.getName());
		}
		*/

	}


	public static ArrayList<Student> quchong(ArrayList<Student> al)
	{
		ArrayList<Student> arrlist=new ArrayList<Student>();
		Iterator<Student> it=al.iterator();
		while(it.hasNext())
		{
			Student s=it.next();
			if(!(arrlist.contains(s)))
				arrlist.add(s);
		}
		return arrlist;

	}

	public static void print(HashMap<String,ArrayList<Student>> hm)
	{
		Set<Map.Entry<String,ArrayList<Student>>> set=hm.entrySet();
		Iterator<Map.Entry<String,ArrayList<Student>>> it=set.iterator();
		while(it.hasNext())
		{
			Map.Entry<String,ArrayList<Student>> me=it.next();
			String key=me.getKey();
			ArrayList<Student> al=me.getValue();
			Iterator<Student> it1=al.iterator();
			while(it1.hasNext())
			{
				Student s=it1.next();
				sop(key+"..."+s.getNo()+"..."+s.getName());
			}
		}
	}

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}



java Map扩展知识练习

标签:

原文地址:http://blog.csdn.net/iemdm1110/article/details/51363950

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!