hashmap使用的练习

package kgc_byme.day17.homework5.tee;

import java.util.*;

/**
 * @Description:
 * @author:myh
 * @date: 2021/6/2  17:23
 */
public class test {
    public static void main(String[] args) {
        String cid;
        Stu stu[]=new Stu[10];
        Map<String, Stu>c1=new HashMap<>();
        Map<String, Stu>c2=new HashMap<>();
        stu[0]=new Stu("1","dsda",77);
        stu[1]=new Stu("2","ds",66);
        stu[2]=new Stu("3","sa",87);
        stu[3]=new Stu("4","cxz",32);
        stu[4]=new Stu("5","dwd",76);
        stu[5]=new Stu("1","myh",66);
        stu[6]=new Stu("2","ef",56);
        stu[7]=new Stu("3","aA",66);
        stu[8]=new Stu("4","KJ",99);
        stu[9]=new Stu("5","AS",77);
        Map<String,Map<String, Stu>> map=new HashMap<>();
        for (int i = 0; i < stu.length; i++) {
            if(i<5){
                c1.put((i+1)+"", stu[i]);
            }else{
                c2.put((i+1-5)+"", stu[i]);
            }
        }

        Stu stux;
        map.put("c1",c1);
        map.put("c2",c2);
        Set<Map.Entry<String, Map<String, Stu>>> entrySetmap = map.entrySet();
        for (Map.Entry<String, Map<String, Stu>> entry : entrySetmap) {

            Map<String, Stu> everyclassStu = entry.getValue();
            System.out.println("班级:"+entry.getKey());
            Set<String> everystuNo = everyclassStu.keySet();
            Iterator<String> iterator = everystuNo.iterator();


            while ((iterator.hasNext())){
                String stuNO=iterator.next();
                System.out.print(stuNO+" ");
                stux=everyclassStu.get(stuNO);
                System.out.println(stux);
            }


        }
        System.out.println("-------手动录入");
        Map<String,Map<String, Stu>> map2=new HashMap<>();
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入");
        int classNums= sc.nextInt();
        for (int i = 1; i <= classNums; i++) {
            System.out.println("输入班级编号");
            String classNo=sc.next();
            System.out.println("每个班级多少人");
            int classStus=sc.nextInt();

            Map<String, Stu> everyClassMap = new HashMap<>();

            for (int i1 = 1; i1 <=classStus; i1++) {
                System.out.println("录入学号");
                String stuNo=sc.next();

                everyClassMap.put(stuNo,new Stu(stuNo,"zs"+i,18+i));
            }

            map2.put(classNo,everyClassMap);

        }
        //遍历m2
        map2.forEach((key,value)->{
            System.out.println("班级编号"+key);
            value.forEach((k,v)->{
                System.out.println("学号"+k);
                System.out.println("对应信息"+v);
            });
        });




    }



}



class Stu{
    String  id;
    String name;
    int score;

    public Stu(String id, String name, int score) {
        this.id = id;
        this.name = name;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Stu{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值