java的Map集合的实现类

文章介绍了Java中HashMap、LinkedHashMap和TreeMap的区别,以及如何使用Comparator对Student对象进行按身高排序。主要展示了如何在Map中存储对象并按照特定属性进行排序。
摘要由CSDN通过智能技术生成

HashMap

 

案例

package com.itheima.d6;

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

public class Test1 {
    public static void main(String[] args) {
        Map<Student, String> map = new HashMap<>();
        map.put(new Student("小明", 2, 1523.0), "碧蓝航线");
        map.put(new Student("小明", 225, 13.0), "明日方舟");
        map.put(new Student("小红", 265, 163.5), "碧蓝档案");
        map.put(new Student("小王 ", 5, 123.0), "地狱公司");
        map.put(new Student("小王 ", 625, 163.0), "地狱公司");
        System.out.println(map);
    }
}
package com.itheima.d6;

import java.util.Objects;

public class Student {
    private String name;
    private int age;
    public double height;

    @Override
    public boolean equals(Object o) {

        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Double.compare(student.height, height) == 0 && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, height);
    }

    public Student() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;

    }

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

LinkedHashMap

TreeMap

 

package com.itheima.d6;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

public class Test33 {
    public static void main(String[] args) {
        Map<Student, String> map = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return Double.compare(o2.getHeight(), o1.getHeight());
            }
        });
        map.put(new Student("小明", 2, 1523.0), "碧蓝航线");
        map.put(new Student("小明", 225, 13.0), "明日方舟");
        map.put(new Student("小红", 265, 163.5), "碧蓝档案");
        map.put(new Student("小王 ", 5, 123.0), "地狱公司");
        map.put(new Student("小王 ", 625, 163.0), "地狱公司");
        System.out.println(map);
    }
}
package com.itheima.d6;

import java.util.Objects;

public class Student implements Comparable<Student>{
    private String name;
    private int age;
    public double height;

    @Override
    public boolean equals(Object o) {

        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Double.compare(student.height, height) == 0 && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, height);
    }

    public Student() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;

    }

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

    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值