java --- nested use of collections

Nested use of collections

案例:
有一个记事本,能记录周一到周天,每天做的所有事情和事情的次数,周而复始。小明在这个记事本上记录了3周,内容如下:
第1周
- 周1:学java,逛街
- 周2:打台球,旅游
- 周3:溜冰,吃烧烤
- 周4:打篮球,逛街,吃烧烤
- 周5:学前端,溜冰
- 周6:逛街,吃烧烤
- 周7:旅游,打台球

第2周:
- 周1:学java,学前端
- 周2:打台球,溜冰,学java
- 周3:旅游,吃烧烤
- 周4:逛街,学java
- 周5:学前端,旅游
- 周6:逛街,打台球,吃烧烤
- 周7:旅游,逛街,打台球,吃烧烤

第3周:
- 周1:学java,学前端,学python
- 周2:打台球,溜冰,学java,学C++
- 周3:旅游,吃烧烤,吃冰淇淋
- 周4:逛街,学java,吃烧烤
- 周5:学前端,旅游,打高尔夫
- 周6:逛街,打台球,吃烧烤,溜冰,蹦迪
- 周7:旅游,逛街,打台球,吃烧烤,跑步

使用Scanner接收小明3周所做的所有事情,并存入相应的集合中。然后输出周一到周五(不区分第几周)小明所做的所有事情以及相应的次数。(要求:所选的集合都要用泛型进行约束。)

If you have a problem, you can know that all events are stored in the map of the first week. However, if the events of Monday of the second week are stored, they will overwrite the events of Monday of the first week. In order to avoid overwriting, we write a method mapaddMap. The events of the two maps are merged so that the events on Monday of the first week and the events on Monday of the second week can be accommodated.
Insert picture description here

Code:

    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        //week共有7个元素
        TreeMap<String, Map<String, Integer>> week = new TreeMap<>();
        //数据录入
        for (int i = 1; i < 4; i++) {
    
    
            System.out.println("第"+i+"周:");
            Map<String,Integer> map;
            for (int j = 1; j < 8; j++) {
    
    
                map = new HashMap<String,Integer>();//每天都会清空昨天的事情和次数
                System.out.println("周"+j+":");
                String thing = sc.next();   //旅游,逛街,旅游,打台球,吃烧烤,跑步
                String[] things_arr = thing.split(",");
                for (String s:things_arr){
    
    
                    //map存入的是:事情和次数
                    map.put(s,map.containsKey(s)?map.get(s)+1:1);
                }

                //第二周,第三周
                if (week.containsKey("周"+j)){
    
    
                    Map<String,Integer> map1=week.get("周"+j);      //假设第二周,得到第二周
                    mapaddMap(map1,map);        //利用mapaddMap方法,将周二map中的事件与第一周map1中的事件相加,
                    // 此时Map集合中的week的map集合就已经发生了改变,为第一周,第二周两周的周一之和
                }else {
    
    
                    //第一周
                    week.put("周"+j,map);
                }
            }
        }

        //数据输出week
        Set<Map.Entry<String, Map<String, Integer>>> weeks = week.entrySet();
        for (Map.Entry<String, Map<String, Integer>> week1:weeks){
    
    
            System.out.println(week1.getKey()+":");
            Map<String, Integer> map = week1.getValue();
            Set<Map.Entry<String, Integer>> things = map.entrySet();
            for (Map.Entry<String, Integer> thing:things){
    
    
                String key = thing.getKey();
                Integer count=thing.getValue();
                System.out.println(key+"---"+count+"次\t");
            }
            System.out.println();//换行
        }
    }

    public static void mapaddMap(Map<String,Integer> map1,Map<String,Integer>map2){
    
    
        Set<String> map2key =map2.keySet();
        for (String m2key:map2key){
    
    
            //融合两个map,将第二周的map融合到第一周map1中,如果map1中有该事件,就让该事件+1,如果没有,就把第二周的事件加入到map1中
            map1.put(m2key,map1.containsKey(m2key)?map1.get(m2key)+map2.get(m2key):map2.get(m2key));
        }
    }

running result:
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44889894/article/details/111621221