第十三节:第四部分:集合框架:HashMap、LinkedHashMap、TreeMap

Map集合体系

HashMap集合的底层原理

HashMap集合底层是基于哈希表实现的

LinkedHashMap集合的底层原理

TreeMap集合的底层原理

代码:

Student类

java 复制代码
package com.itheima.day26_Map_impl;

import java.util.Objects;

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", height=" + 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(height, student.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 int compareTo(Student o) {
        return this.age - o.age;//年龄升序排序
    }
}

代码一:掌握Map集合下的实现类:HashMap集合的底层原理

java 复制代码
package com.itheima.day26_Map_impl;

import java.util.HashMap;

//目标:掌握Map集合下的实现类:HashMap集合的底层原理
public class HashMapTest1 {
    public static void main(String[] args) {
        HashMap<Student,String> map = new HashMap<>();
        map.put(new Student( "蜘蛛精",25,168.5),"盘丝洞");
        map.put(new Student( "蜘蛛精",25,168.5),"水帘洞");
        map.put(new Student( "至尊宝",27,178.5),"水帘洞");
        map.put(new Student( "牛魔王",28,188.5),"牛头山");
        System.out.println(map);

    }
}

代码二:目标:掌握LinkedHashMap的底层原理

java 复制代码
package com.itheima.day26_Map_impl;

import java.util.LinkedHashMap;
import java.util.Map;

//目标:掌握LinkedHashMap的底层原理
public class LinkedHashMapTest1 {
    public static void main(String[] args) {
        Map<String,Integer> map = new LinkedHashMap<>();//按照键 有序,不重复,无索引。
        map.put("手机",128);
        map.put("手机",222);
        map.put("笔记本",666);
        map.put("手表",999);
        map.put(null,null);
        System.out.println(map);
    }
}

代码三:掌握TreeMap集合的使用

java 复制代码
package com.itheima.day26_Map_impl;

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

//目标:掌握TreeMap集合的使用
public class TreeMapTest {
    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(o1.getHeight(), o2.getHeight());
            }
        });
        map.put(new Student( "蜘蛛精",25,168.5),"盘丝洞");
        map.put(new Student( "蜘蛛精",25,168.5),"水帘洞");
        map.put(new Student( "至尊宝",27,178.5),"水帘洞");
        map.put(new Student( "牛魔王",28,188.5),"牛头山");
        System.out.println(map);
    }
}
相关推荐
大模型玩家七七9 分钟前
基于语义切分 vs 基于结构切分的实际差异
java·开发语言·数据库·安全·batch
寻星探路5 小时前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
曹牧7 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
爬山算法8 小时前
Hibernate(90)如何在故障注入测试中使用Hibernate?
java·后端·hibernate
kfyty7258 小时前
集成 spring-ai 2.x 实践中遇到的一些问题及解决方案
java·人工智能·spring-ai
猫头虎8 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
李少兄8 小时前
在 IntelliJ IDEA 中修改 Git 远程仓库地址
java·git·intellij-idea
忆~遂愿9 小时前
ops-cv 算子库深度解析:面向视觉任务的硬件优化与数据布局(NCHW/NHWC)策略
java·大数据·linux·人工智能
小韩学长yyds9 小时前
Java序列化避坑指南:明确这4种场景,再也不盲目实现Serializable
java·序列化