Day20 HashMap的基本操作

HashMap的基本操作

<------------------------创建学生类------------------------->
public class Student implements Comparable<Student>{
    private int age;
    private String name;
    private double score;
    private int sno;

    public Student() {
    }

    public Student(int age, String name, double score, int sno) {
        this.age = age;
        this.name = name;
        this.score = score;
        this.sno = sno;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public int getSno() {
        return sno;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    @Override
    public int compareTo(Student other) {//内部比较器只能定义一个,频率最高的使用
        return this.sno - other.sno;
        //return -(this.sno - other.sno);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (age != student.age) return false;
        if (Double.compare(student.score, score) != 0) return false;
        if (sno != student.sno) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = age;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        temp = Double.doubleToLongBits(score);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + sno;
        return result;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", score=" + score +
                ", sno=" + sno +
                '}';
    }
}


<------------------------创建测试类------------------------->
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*
* 使用Map存储学号和学生的映射
*
* */
public class TestMap2 {
    public static void main(String[] args) {

        Map<Integer,Student> map  = new HashMap<Integer,Student>();
        //HashMap<Integer,Student> map  = new HashMap<Integer,Student>();这样再去找isempty的源码


        Student stu1 = new Student(20,"张三",80,1);
        Student stu2 = new Student(19,"李四",81,2);
        Student stu3 = new Student(18,"王五",84,3);
        Student stu4 = new Student(21,"赵六",85,4);
        Student stu5 = new Student(20,"张三",80,1);

        map.put(stu1.getSno(),stu1);
        map.put(stu2.getSno(),stu2);
        map.put(stu3.getSno(),stu3);
        map.put(stu4.getSno(),stu4);
        map.put(stu5.getSno(),stu5);


        //其他方法
        //map.clear();//全部清空
        //map.remove(3);
        //map.isEmpty();
        //System.out.println(map.containsKey(5));包含是否学号为5的学生

        System.out.println(map.get(3));

        //遍历输出
        Set<Map.Entry<Integer,Student>> entryset =  map.entrySet();
        Iterator<Map.Entry<Integer,Student>> it  = entryset.iterator();
        while(it.hasNext()){
            Map.Entry<Integer,Student> entry = it.next();
            //System.out.println(entry);
            Student stu = entry.getValue();
            System.out.println(stu);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值