暑期JAVA学习(14.2)Map集合的实现类

在这里插入图片描述

建议结合前面的”暑期JAVA学习(10)Set系列集合“一起看,理解效果更好哦~

一、Map集合的实现类HashMap

(1)HashMap集合概述和特点

●HashMap是Map里面的一个实现类。特点都是由键决定的:无序、不重复、无索引

●没有额外需要学习的特有方法,直接使用Map里面的方法就可以了。

●HashMap跟HashSet底层原理是一模一样的,都是哈希表结构,只是HashMap的每个元素包含两个值而已。

●依赖hashCode方法和equals方法保证的唯一。

●如果键要存储的是自定义对象,需要重写hashCode和equals方法。

●基于哈希表。增删改查的性能都较好。

实际上:Set系列集合的底层就是Map实现的,只是Set集合中的元素只要键数据,不要值数据而已。
在这里插入图片描述

(2)HashMap集合特点代码体现

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

public class MapDemo01 {
    public static void main(String[] args) {
        // Map集合去重复原因:先判断哈希值再判断equals
        Map<Student,Integer> students = new LinkedHashMap<>();
        Summerday09.Student s1 = new Summerday09.Student("小红",20,'女');
        Summerday09.Student s2 = new Summerday09.Student("小红",20,'女');
        Summerday09.Student s3 = new Student("小王",21,'男');
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
        System.out.println(s3.hashCode());

        //依赖hashCode方法和equals方法保证键的唯一。
        students.put(s1,1);
        students.put(s2,2);
        students.put(s3,3);

        System.out.println(students);

    }
}

public class Student {
    private String name;
    private int age;
    private char sex;

    public Student() {
    }

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

    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 char getSex() {
        return sex;
    }

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

    //重写ToString方法,否则返回的是地址,不是内容
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                '}';
    }

    /**
     * 只要2个对象内容一样, 结果一定是true
     * @param o
     * @return
     */
    @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 && sex == student.sex && Objects.equals(name, student.name);
    }

    /**
     *         Student s1 = new Student("小红",20,'女');
     *         Student s2 = new Student("小红",20,'女');
     *         Student s3 = new Student("小王",21,'男');
     * @return
     */
    @Override
    public int hashCode() {
        return Objects.hash(name, age, sex);
    }
}

二、Map集合的实现类LinkedHashMap

(1)LinkedHashMap集合概述和特点

●由键决定:有序、不重复、无索引。
●这里的有序指的是保证存储和取出的元素顺序一致
●原理:底层数据结构是依然哈希表,只是每个键值对元素又额外的多了一个双链表的机制记录存储的顺序。
在这里插入图片描述
在这里插入图片描述

(2)LinkedHashMap集合特点代码体现

在这里插入图片描述
在这里插入图片描述

public class MapDemo02 {
    public static void main(String[] args) {
        //添加元素:有序,不重复,无索引。
        Map<String,Integer> maps = new LinkedHashMap<>();
        maps.put ("iphoneX",10);
        maps.put("娃娃",31);
        maps.put ("iphoneX",100);// Map集合后面重复的键对应的元素会覆盖前面重复的整个元素!
        maps.put("huawei",1000);
        maps.put("生活用品",10);
        maps.put("手表",10);
        System.out.println(maps);
        //{huawei=1000, 手表=10, 生活用品=10, iphoneX=100, 娃娃=31}

        }
    }

三、Map集合的实现类TreeMap

(1)TreeMap集合概述和特点

●由键决定特性:不重复、无索引、可排序
●可排序:按照键数据的大小默认升序(有小到大)排序。只能对键排序。
●注意:TreeMap集合是一定要排序的,可以默认排序,也可以将键按照指定的规则进行排序
●TreeMap跟TreeSet样底层原理是一样的。

(2)TreeMap集合默认排序代码体现

在这里插入图片描述在这里插入图片描述

public class MapDemo03 {
    public static void main(String[] args) {
        //目标:观察TreeMap对于有值特性的数据如何排序。.
        //学会对自定义类型的对象进行指定规则排序
        //按照键数据的大小默认升序(有小到大)排序。只能对键排序。
        Map<Integer,Integer> maps = new TreeMap<>();//不重复,无索引,可排序
        maps.put(25,1);
        maps.put(25,2);
        maps.put(10,3);
        maps.put(0,4);
        maps.put(99,5);
        System.out.println(maps);

        Map<String,Integer> maps1 = new TreeMap<>();//不重复,无索引,可排序
        maps1.put("Java",1);
        maps1.put("Java",2);
        maps1.put("java",3);
        maps1.put("apple",4);
        maps1.put("Phthon",5);
        maps1.put("Apple",6);
        System.out.println(maps1);

    }
}
(3)TreeMap集合自定义排序规则有2种
①类实现Comparable接口, 重写比较规则

在这里插入图片描述
在这里插入图片描述在这里插入图片描述
输出结果:
{Apple{name=‘红富士’, color=‘红色’, price=9.9, weight=500}=陕西, Apple{name=‘青苹果’, color=‘青色’, price=19.9, weight=600}=山东, Apple{name=‘红玉’, color=‘红色’, price=15.8, weight=1000}=江南}

public class TreeMapDemo01 {
    public static void main(String[] args) {
        //可排序不重复(只要大小规则一样就认为重复) 无索引
        Map<Apple,String> maps = new TreeMap<>();
        maps.put(new Apple("红富士","红色",9.9,500),"江西");
        maps.put(new Apple("红将军","红色",15.0,500),"陕西");
        maps.put(new Apple("青苹果","青色",19.9,600),"山东");
        maps.put(new Apple("红玉","红色",15.8,1000),"江南");
        System.out.println(maps);
        //{Apple{name='红富士', color='红色', price=9.9, weight=500}=陕西,
        // Apple{name='青苹果', color='青色', price=19.9, weight=600}=山东,
        // Apple{name='红玉', color='红色', price=15.8, weight=1000}=江南}

    }
}

public class Apple implements Comparable<Apple>{
    private String name;
    private String color;
    private double price;
    private int weight;

    public Apple() {
    }

    public Apple(String name, String color, double price, int weight) {
        this.name = name;
        this.color = color;
        this.price = price;
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;

    }

    @Override
    public String toString() {
        return "Apple{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                ", price=" + price +
                ", weight=" + weight +
                '}';
    }

    /**
     * 方式一:类自定义比较规则
     * @param o
     * @return
     */
    @Override
    public int compareTo(Apple o) {
        //按照重量进行比较
        return this.weight - o.weight;
    }
}

②集合自定义Comparator比较器对象,重写比较规则。

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
输出结果:{Apple{name=‘青苹果’, color=‘青色’, price=19.9, weight=600}=山东, Apple{name=‘红玉’, color=‘红色’, price=15.8, weight=1000}=江南, Apple{name=‘红将军’, color=‘红色’, price=15.0, weight=500}=陕西, Apple{name=‘红富士’, color=‘红色’, price=9.9, weight=500}=江西}

public class TreeMapDemo02 {
    public static void main(String[] args) {
        //可排序不重复(只要大小规则一样就认为重复) 无索引
        //方式二:集合自带比较器对象进行规则定制
        Map<Apple,String> maps = new TreeMap<>(new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                //return o1.getWeight() - o2.getWeight();//升序//重量相同时,会去掉重量相同的元素
                //return o2.getWeight() - o1.getWeight();//降序//重量相同时,会去掉重量相同的元素
                //return o2.getWeight() - o1.getWeight() >= 0 ? 1 :-1;//重量相同时,不会去掉重量相同的元素
                //注意:浮点型建议直接使用Double. compare进行比较
                //return Double.compare(o2.getPrice() , o1.getPrice());//升序
                return Double.compare(o2.getPrice(),o1.getPrice());//按照价格降序
            }
        });
        maps.put(new Apple("红富士","红色",9.9,500),"江西");
        maps.put(new Apple("红将军","红色",15.0,500),"陕西");
        maps.put(new Apple("青苹果","青色",19.9,600),"山东");
        maps.put(new Apple("红玉","红色",15.8,1000),"江南");
        System.out.println(maps);
        //{Apple{name='青苹果', color='青色', price=19.9, weight=600}=山东,
        // Apple{name='红玉', color='红色', price=15.8, weight=1000}=江南,
        // Apple{name='红将军', color='红色', price=15.0, weight=500}=陕西,
        // Apple{name='红富士', color='红色', price=9.9, weight=500}=江西}
    }
}
public class Apple implements Comparable<Apple>{
    private String name;
    private String color;
    private double price;
    private int weight;

    public Apple() {
    }

    public Apple(String name, String color, double price, int weight) {
        this.name = name;
        this.color = color;
        this.price = price;
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

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

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;

    }

    @Override
    public String toString() {
        return "Apple{" +
                "name='" + name + '\'' +
                ", color='" + color + '\'' +
                ", price=" + price +
                ", weight=" + weight +
                '}';
    }

    /**
     * 方式一:类自定义比较规则
     * @param o
     * @return
     */
    @Override
    public int compareTo(Apple o) {
        //按照重量进行比较
        return this.weight - o.weight;
    }
}

三、Map集合实现类特点总括

●HashMap:元素按照键是无序,不重复,无索引,值不做要求,基于哈希表(与Map体系一致)
●LinkedHashMap:元素按照键是有序, 不重复,无索引,值不做要求,基于哈希表
●TreeMap:元素只能按照键排序,不重复,无索引的,值不做要求,可以做排序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呦呦呦欸哟哟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值