其他分享
首页 > 其他分享> > 集合嵌套案例

集合嵌套案例

作者:互联网

package Maptest01;

import java.util.*;

/**
 * FileName: MapDemo04
 * Author:   lps
 * Date:     2022/4/16 13:20
 * Sign:刘品水 Q:1944900433
 */
public class MapDemo04 {
    public static void main(String[] args) {
        //1.要求程序记录每个学生选择的情况
        //使用map集合存储
        Map<String, List<String>> data=new HashMap<>();
        
        //2.把学生的选择存入进去
        List<String> selects = new ArrayList<>();
        Collections.addAll(selects,"A","C");
        data.put("刘品水",selects);
        List<String> selects1 = new ArrayList<>();
        Collections.addAll(selects1,"C","D");
        data.put("张伟",selects1);
        List<String> selects2 = new ArrayList<>();
        Collections.addAll(selects2,"A","B","C","D");
        data.put("音泽峰",selects2);
        System.out.println(data);

        //3.统计每个经典选择人数
        Map<String,Integer> infos = new HashMap<>();

        //提取所有人选择经典的信息
        Collection<List<String>> values = data.values();
        System.out.println(values);
        //[[A, B, C, D], [C, D], [A, C]]

        for (List<String> value : values) {
            for (String s : value) {
                if (infos.containsKey(s)){
                    infos.put(s,infos.get(s)+1);
                }else {
                    infos.put(s,1);
                }
            }
        }
        System.out.println("========");
        System.out.println(infos);



    }
}

 

标签:案例,List,put,System,嵌套,集合,new,infos,data
来源: https://www.cnblogs.com/lps1944900433/p/16152594.html