本帖最后由 JACK...... 于 2013-12-22 17:00 编辑
- <DIV class=blockcode>
- <BLOCKQUOTE>
- <P>import java.util.*;
- import java.util.Map.Entry;
- public class DemoExtend{
- public static void main(String[] args){
- strlist();
- System.out.println("*********************");
- strmap();
- }
- //一对多Map集合。HashMap中嵌套HashMap。
- public static void strmap(){
- HashMap<String,HashMap<String,String>> DH=new HashMap<String,HashMap<String,String>>();
- HashMap<String,String> base=new HashMap<String,String>();
- HashMap<String,String> jiuye=new HashMap<String,String>();
- //信息录入
- base.put("02", "lisi");
- base.put("01", "zhangsan");
- jiuye.put("01", "zhangba");
- jiuye.put("02", "liqi");
- //建立关系
- DH.put("base", base);
- DH.put("jiuye", jiuye);
- //迭代器嵌套输出。
- /*Iterator<String> I=DH.keySet().iterator();
- while(I.hasNext()){
- String room=I.next();
- Iterator<String> i=DH.get(room).keySet().iterator();
- while(i.hasNext()){
- String id=i.next();
- String name=DH.get(room).get(id);
- System.out.println(id+"...."+name);
- }
- }*/</P>
- <P> </P>
- <P>//此处,我想用Map.Entry来实现嵌套输出</P>
- <P>//可是遇到了问题。
- //Set<Map.Entry<String, HashMap<String, String>>> test=DH.entrySet();
- //Iterator<Entry<String, HashMap>>=test.iterator();
- Iterator<Map.Entry<String, HashMap<String, String>>> I=DH.entrySet().iterator();
- //****************************************************************************************
- }
- //HashMap中嵌套list
- public static void strlist(){
- //建立学校集合
- HashMap<String,List<stu>> HD=new HashMap<String,List<stu>>();
- List<stu> base=new ArrayList<stu>();
- List<stu> admin=new ArrayList<stu>();
- base.add(new stu("01","zhangsan"));
- base.add(new stu("02","liusan"));
- admin.add(new stu("02","liusan"));
- admin.add(new stu("01","zhangli"));
- //建立关系
- HD.put("预热班", base);
- HD.put("就业班", admin);
- /*
- Iterator<String> I=HD.keySet().iterator();
- while(I.hasNext()){
- //获取,学校中的键,也就是,班级的名字
- String room=I.next();
- //搭建list迭代器
- Iterator<stu> i=HD.get(room).iterator();
- while(i.hasNext()){
- stu student=i.next();
- System.out.println(student.getxn());
- }
- }*/
- }
- }
- //学生类
- class stu{
- private String xuehao;
- private String name;
- stu(String xuehao,String name){
- this.xuehao=xuehao;
- this.name=name;
- }
- public String getxn(){
- return xuehao+":"+name;
- }
- }
- </P>
复制代码
问题:请提供一个用Map.Entry嵌套while循环的信息输出?有劳
|