java map扩展
2020-04-08 16:09:24来源:博客园 阅读 ()
java map扩展
map集合被使用是因为具备映射关系。
一个学校有多个教室,每个教室都有多个学生。
public class Demo { public static void main(String[] args) { HashMap<String,HashMap<String,String>> czbk = new HashMap<String,HashMap<String,String>>(); HashMap<String,String> yure = new HashMap<String,String>(); yure.put("01","xiaoming"); yure.put("02","xiaohong"); HashMap<String,String> jiuye = new HashMap<String,String>(); jiuye.put("01","xiaoxue"); jiuye.put("02","xiaoli"); czbk.put("jiuyeban",jiuye); czbk.put("yureban",yure); getInfo(czbk); } public static void getStudentInfo(HashMap<String ,String> roomMap){ Iterator<String> it = roomMap.keySet().iterator(); while (it.hasNext()){ String id = it.next(); String name = roomMap.get(id); System.out.println(id+":"+name); } } public static void getInfo(HashMap<String,HashMap<String,String>> school){ Iterator<String> it = school.keySet().iterator(); while (it.hasNext()){ String roomName = it.next(); HashMap<String,String> room = school.get(roomName); System.out.println(roomName); getStudentInfo(room); } } }
class Student { private int id; private String name; public Student(int id, String name) { this.id = id; this.name = name; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + '}'; } } public class Demo { public static void main(String[] args) { HashMap<String, List<Student>> czbk = new HashMap<String, List<Student>>(); List<Student> yure = new ArrayList<Student>(); List<Student> jiuye = new ArrayList<Student>(); czbk.put("yureban", yure); czbk.put("jiuyeban", jiuye); yure.add(new Student(1, "xiaohong")); yure.add(new Student(2, "xiaoli")); jiuye.add(new Student(1, "xiaoxue")); jiuye.add(new Student(2, "xiaomeng")); Iterator<String> it = czbk.keySet().iterator(); while (it.hasNext()) { String roomName = it.next(); List<Student> room = czbk.get(roomName); System.out.println(roomName); getInfos(room); } } public static void getInfos(List<Student> room) { Iterator<Student> it = room.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } }
原文链接:https://www.cnblogs.com/hongxiao2020/p/12661300.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash