第十三节:第四部分:集合框架: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);
    }
}
相关推荐
爬山算法1 分钟前
Hibernate(85)如何在持续集成/持续部署(CI/CD)中使用Hibernate?
java·ci/cd·hibernate
菜鸟233号35 分钟前
力扣647 回文子串 java实现
java·数据结构·leetcode·动态规划
qq_124987075341 分钟前
基于Java Web的城市花园小区维修管理系统的设计与实现(源码+论文+部署+安装)
java·开发语言·前端·spring boot·spring·毕业设计·计算机毕业设计
h7ml1 小时前
查券返利机器人的OCR识别集成:Java Tesseract+OpenCV优化图片验证码的自动解析方案
java·机器人·ocr
野犬寒鸦1 小时前
从零起步学习并发编程 || 第五章:悲观锁与乐观锁的思想与实现及实战应用与问题
java·服务器·数据库·学习·语言模型
Volunteer Technology1 小时前
Sentinel的限流算法
java·python·算法
岁岁种桃花儿1 小时前
SpringCloud从入门到上天:Nacos做微服务注册中心
java·spring cloud·微服务
jdyzzy1 小时前
什么是 JIT 精益生产模式?它与传统的生产管控方式有何不同?
java·大数据·人工智能·jit
Chasmれ1 小时前
Spring Boot 1.x(基于Spring 4)中使用Java 8实现Token
java·spring boot·spring
汤姆yu1 小时前
2026基于springboot的在线招聘系统
java·spring boot·后端