2019年2月22日,集合框架——List、Set、Map作业

java编写源文件地址为:

链接:https://pan.baidu.com/s/1jeScQwPd6J1MsTYkwWxriw
提取码:102x
复制这段内容后打开百度网盘手机App,操作更方便哦

作业:

本题目使用Map集合;

编写Student类:

package com.jihe.zuoye;

public class Student {
    private String name;
    private String sex;

    public Student() {
    }

    public Student(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

编写StudentMap类:

package com.jihe.zuoye;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class StudentMap {
    public static void main(String[] args) {
        Student stu1 = new Student("张三","男");
        Student stu2 = new Student("王四","女");
        Student stu3 = new Student("赵五","男");

        Map<String,Student> map = new HashMap<String,Student>();
        map.put("Jo",stu1);
        map.put("Lily",stu2);
        map.put("Smith",stu3);

        Set<String> keys = map.keySet();
        for(String key:keys){
            System.out.println(key+"的中文名为:"+map.get(key).getName()+",性别为:"+map.get(key).getSex());

        }

    }
}

输出结果为:

Jo的中文名为:张三,性别为:男
Smith的中文名为:赵五,性别为:男
Lily的中文名为:王四,性别为:女

 

posted @ 2019-02-22 20:07  BOZHU-liu  阅读(263)  评论(0编辑  收藏  举报