Java-Map集合的应用-一对多的映射…

一对多:

一个教室对应多个学生.

<wbr></wbr>

学校里边不同的班级有不同的学生,但是可能ID相同.

<wbr></wbr>

//优化过后的代码,更具有复用和扩展性.

//定义一个可以存放三个数据的集合——班级名称,学生ID,学生名称
import java.util.*;
class MyMap
{
<wbr>private Map map;<br><wbr>MyMap()<br><wbr>{<br><wbr><wbr>map = new HashMap();<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>public void createClass(String className, String id, String name)<br><wbr>{<br><wbr><wbr>Map subMap = (Map)map.get(className);<br><wbr><wbr>if (subMap == null)<br><wbr><wbr>{<wbr>subMap = new HashMap();<br><wbr><wbr><wbr>map.put(className, subMap);<wbr> //已经存在班级,只需要向学生这个Map集合里边存数据,而班级的Map不需要再存了<br><wbr><wbr>}<br><wbr><wbr>subMap.put(id, name);<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>public String getName(String className, String id)<br><wbr>{<wbr><wbr><br><wbr><wbr>Map subMap = (Map)map.get(className);<br><wbr><wbr>return (String)subMap.get(id);<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>public void getStudent(String className)<br><wbr>{<wbr><br><wbr><wbr>System.out.println(className);<br><wbr><wbr>Map subMap = (Map)map.get(className);<br><wbr><wbr>Set mySet = subMap.keySet();<br><wbr><wbr>for (Iterator it = mySet.iterator(); it.hasNext(); )<br><wbr><wbr>{<br><wbr><wbr><wbr>String id = (String)it.next();<br><wbr><wbr><wbr>String name = (String)subMap.get(id);<br><wbr><wbr><wbr>System.out.println(id + "..." + name);<br><wbr><wbr>}<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>public void getAll()<br><wbr>{<wbr><wbr><br><wbr><wbr>Set set1 = map.keySet();<br><wbr><wbr>for (Iterator it = set1.iterator(); it.hasNext(); )<br><wbr><wbr>{<br><wbr><wbr><wbr>String className = (String)it.next();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr>System.out.println(className);</wbr></wbr></wbr>

<wbr><wbr><wbr>Map subMap = (Map)map.get(className);<br><wbr><wbr><wbr>Set set2 = subMap.keySet();<br><wbr><wbr><wbr>for (Iterator it1 = set2.iterator(); it1.hasNext(); )<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>String id = (String)it1.next();<br><wbr><wbr><wbr><wbr>String name = (String)subMap.get(id);<br><wbr><wbr><wbr><wbr>System.out.println(it + "..." + name);<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr>}<br> }<br> class MyMapDemo<br> {<br><wbr>public static void main(String[] args)<br><wbr>{<wbr><br><wbr><wbr>MyMap mm = new MyMap();<br><wbr><wbr>mm.createClass("班级一", "01", "小张");<br><wbr><wbr>mm.createClass("班级一", "02", "小王");<br><wbr><wbr>mm.createClass("班级一", "03", "小李");<br><wbr><wbr>mm.createClass("班级二", "01", "小A");<br><wbr><wbr>mm.createClass("班级二", "02", "小B");<br><wbr><wbr>mm.createClass("班级二", "03", "小C");<br><wbr><wbr>//System.out.println(mm.getName("班级一", "02"));</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>//mm.getStudent("班级一");<br><wbr><wbr>//mm.getStudent("班级二");<br><wbr><wbr>mm.getAll();<br><wbr>}<br> }<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr></wbr>

//前期设计代码,不具备扩展性和复用性.
import java.util.*;
class MapDemo
{
<wbr>public static void main(String[] args)<br><wbr>{<wbr><br><wbr><wbr>//初期设计————(代码缺少复用性,冗余)<br><wbr><wbr>//定义一个学校,里边可以有多个班级<br><wbr><wbr>HashMap &lt;String, HashMap&gt; school = new HashMap&lt;String, HashMap&gt;();<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>//班级一<br><wbr><wbr>HashMap&lt;String, String&gt; class1 = new HashMap&lt;String ,String&gt;();<br><wbr><wbr>class1.put("01", "小张");<br><wbr><wbr>class1.put("02", "小王");<br><wbr><wbr>class1.put("03", "小李");<wbr><wbr><br><wbr><wbr>//班级二<br><wbr><wbr>HashMap&lt;String, String&gt; class2 = new HashMap&lt;String ,String&gt;();<br><wbr><wbr>class2.put("01", "小周");<br><wbr><wbr>class2.put("02", "小孙");<br><wbr><wbr>class2.put("03", "小赵");</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr> school.put("班级一", class1);<br><wbr><wbr>school.put("班级二", class2);<br><wbr><wbr>show(school);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>}<br><wbr>public static void show(Map map)<br><wbr>{<br><wbr><wbr>Set&lt;String&gt; s = map.keySet();<br><wbr><wbr>for (Iterator&lt;String&gt; it = s.iterator(); it.hasNext(); )<br><wbr><wbr>{<br><wbr><wbr><wbr>String className = it.next();<br><wbr><wbr><wbr>System.out.println(className);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr>Map hs = (Map)map.get(className);<br><wbr><wbr><wbr>Set &lt;String&gt; s1 = hs.keySet();<br><wbr><wbr><wbr>for (Iterator &lt;String&gt; it1 = s1.iterator(); it1.hasNext(); )<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>String id = it1.next();<br><wbr><wbr><wbr><wbr>String name = (String)hs.get(id);<br><wbr><wbr><wbr><wbr>System.out.println(id+".."+name);<br><wbr><wbr><wbr>}<br><wbr><wbr>}<br><wbr>}<br> }<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值