一个集合的嵌套,快把我搞疯了,代码不难,主要是还是两种构造器的原理不熟,走了些弯路子。
大神有能力可以三层嵌套。绝对考验智力,验证下你的头脑是否还清醒。
- import java.util.*;
- import java.util.Map.Entry;
- public class Demo
- {
- public static void main(String[] args)
- {
-
- HashMap<String, HashMap<String, String>> 传智博客=new HashMap<String, HashMap<String, String>>();
- HashMap<String,String> 基础班=new HashMap<String,String>();
- HashMap<String,String> 就业班=new HashMap<String,String>();
- 基础班.put("001", "zhangsan1");
- 基础班.put("002", "zhangsan2");
- 基础班.put("003", "zhangsan3");
- 就业班.put("001", "lisi1");
- 就业班.put("002", "lisi2");
- 就业班.put("003", "lisi3");
- 传智博客.put("基础教学", 基础班);
- 传智博客.put("企业教学", 就业班);
- keySet(传智博客);
-
- }
- //entrySet方式获取
- public static void entrySet(HashMap<String,HashMap<String,String>> 传智博客){
- Set<Map.Entry<String,HashMap<String,String>>> set=传智博客.entrySet();
- Iterator <Map.Entry<String,HashMap<String,String>>> it=set.iterator();
- while(it.hasNext()){
- Map.Entry<String,HashMap<String,String>> cz=it.next();
- String key=cz.getKey();
- HashMap<String,String> value =cz.getValue();
- System.out.println(key+":");
-
- Set<Entry<String, String>> set2=value.entrySet();
- Iterator <Map.Entry<String,String>> it2=set2.iterator();
- while(it2.hasNext()){
- Map.Entry<String, String> cz2=it2.next();
- String key2=cz2.getKey();
- String value2=cz2.getValue();
- System.out.println(key2+"..."+value2);
- }
- }
- }
- //KeySet方式获取
- public static void keySet(HashMap<String, HashMap<String, String>> 传智博客){
- Set<String> set=传智博客.keySet();
- Iterator<String> it=set.iterator();
- while(it.hasNext()){
- String key=it.next();
- HashMap<String,String> map=传智博客.get(key);
- System.out.println(key+":");
-
- Set<String> set2=map.keySet();
- Iterator <String> it2=set2.iterator();
- while(it2.hasNext())
- {
- String key2=it2.next();
- String map2=map.get(key2);
- System.out.println(key2+"+++"+map2);
- }
- }
- }
- }
- //学生类
- class Student
- {
- private String name;
- private String xuehao;
- public void stady(){
- System.out.println("学习");
- }
- public Student(){}
- public Student(String name,String xuehao){
- this.name=name;
- this.xuehao=xuehao;
- }
- public boolean equals(Object obj){
- if(this==obj)
- return true;
- if(obj==null)
- return false;
- if(obj instanceof Student)
- {
- Student stu=(Student)obj;
- return this.name.equals(stu.name);
- }
- return false;
- }
- public int hashCode(){
- return this.name.hashCode()+this.xuehao.hashCode();
- }
- public String toString(){
- return "Student: "+name+"..."+xuehao;
- }
- //----------------gs-----------------------
- public void setName(String name){
- this.name=name;
- }
- public String getName(){
- return name;
- }
- public void setXuehao(String xuehao){
- this.xuehao =xuehao;
- }
- public String getXuehao(){
- return xuehao;
- }
- }
- //基础班学生类
- class JCStudent extends Student
- {
- public void stady(){
- System.out.println("基础学习");
- }
- }
- //就业班学生类
- class JYStudent extends Student
- {
- public void stady(){
- System.out.println("就业学习");
- }
- }
复制代码
|