Java基础知识 22(Map,HashMap及其遍历(键找值,键值对对象,forEach)LinkedHashMap,TreeMap及其排序,HashMap 和Hashtable 区别,集合的嵌套)

Java基础知识 22

在此之前,我们已经学习Collection集合的子集合,Set集合的子集合,接下来我们要学习Map集合以及它的子集合,我们先用一张图来表示一下它们之间的关系以及需要注意的知识点:
在这里插入图片描述

Map集合

在生活中这种键值对应的的数据很常见,那么Java为了我们方便的去操作这种键值对应关系的数据,给我们提供了另外一种集合叫做Map集合,专门用来操作这种键值对应的数据。
键值对应关系的数据是一个键只能映射到一个值。
Map 将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
多次存储相同的键,就会发生键相同值覆盖的问题。

import java.util.HashMap;

public class Mytest {
    public static void main(String[] args) {
        //存储键是String类型,值是student类型
        HashMap<String, Student> hm = new HashMap<>();
        hm.put("0x001",new Student("小明",23));
        //后一个把前一个覆盖了:多次存储相同的键,就会发生键相同值覆盖
        hm.put("0x001",new Student("小绿",21));
        hm.put("0x002",new Student("小红",24));
        hm.put("0x003",new Student("小花",25));
        hm.put("0x004",new Student("小黄",26));
        System.out.println(hm);
    }
}

Map接口和Collection接口的不同:
Map集合是双列集合,Collection集合是单列的。
Map的键是唯一的,Collection的子体系是唯一的。
Map集合的数据结构针对键有效,跟值无关。
Collection集合的数据结构是针对元素有效。

HashMap集合

Hashmap键的数据结构是哈希表,所以键是无序的,键的唯一性要靠重写hashCode()和equals()方法才能保证,否则没有唯一性。
功能概述:
(1)添加功能:put();添加元素,这个键还有一个功能:替换。
如果是第一次存储,就直接存储元素,返回null。
如果不是第一次存储,就用新值把以前的值替换掉,返回以前的值。

(2)清空集合:clear();
(3)remove() : 根据键来删除一对键值对,返回的是这个键对应的这个值。

public class Mytest {
    public static void main(String[] args) {
        /*
         * Map集合:就是用来存储这种键值映射关系的数据。Map是双列集合
         * 一个键只能存储一个值,键是唯一的。
         * 多次存储相同的键,就会发生键相同值覆盖。
         * Map集合的数据结构只跟键有关,跟值没关系。
         * collection是单列集合
         */
        //HashMap键的数据结构是哈希表,键的唯一性要靠键重写hashCode()方法和equals()方法,才能保证键的唯一性。
        HashMap<String, Student> hm = new HashMap<String, Student>();
        hm.put("0x001",new Student("孙悟空",45));
        hm.put("0x002",new Student("唐僧",50));
        hm.put("0x003",new Student("沙悟净",48));
        hm.put("0x004",new Student("猪八戒",47));
        hm.put("0x001",new Student("孙悟空",500));

        System.out.println(hm.toString());
        System.out.println("---------------------");
        //根据键来删除一对键队值,返回的是这个键对应的值
        Student s1 = hm.remove("0x003");
        System.out.println(s1);
        System.out.println(hm.toString());
        System.out.println("---------------------");
        boolean b = hm.remove("0x002", new Student("唐僧", 50));
        System.out.println(b);

    }
}
------------------------------------------
public class Student {
    private String name;
    private int age;

    public Student() {
    }

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

    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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" +
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值