一对多:
一个教室对应多个学生.
<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 <String, HashMap> school = new HashMap<String, HashMap>();<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>//班级一<br><wbr><wbr>HashMap<String, String> class1 = new HashMap<String ,String>();<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<String, String> class2 = new HashMap<String ,String>();<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<String> s = map.keySet();<br><wbr><wbr>for (Iterator<String> 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 <String> s1 = hs.keySet();<br><wbr><wbr><wbr>for (Iterator <String> 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>