1. 程式人生 > 其它 >2022.5.12 AcWing每日一題

2022.5.12 AcWing每日一題

HashMap

概述

HashMap是Map介面的十分重要的實現類
底層實現是陣列+連結串列+紅黑樹
特點:無序,無腳標,鍵不可重複,值可重複

實踐

如果HashMap的key或value是自定義類,想要兩個內容相同的例項定義為同一個例項,需要重寫hashcode和equals方法,總結:包含hash的集合元素如果是自定義類,內容相同的例項想要定義為一個例項,需要重寫hashcode和equals,比如HashSet和HashMap;List實現類僅需要重寫equals方法;Tree相關集合僅需重寫比較方法

package com.qianfeng.collection;

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

/**
 * 功能描述
 *
 * @since 2022-05-16
 */
public class HashMapDemo {
    public static void main(String[] args) {
        Student1 student1 = new Student1("zhangsan", 1);
        Student1 student2 = new Student1("lisi", 2);
        Student1 student3 = new Student1("wangwu", 3);
        HashMap<Student1, String> scores = new HashMap<>();
        scores.put(student1, "89");
        scores.put(student2, "80");
        scores.put(student3, "78");
        System.out.println(scores);

        scores.remove(student1);

        for (Student1 score : scores.keySet()) {
            System.out.println(score + "========" + scores.get(score));
        }

        for (Map.Entry entry : scores.entrySet()) {
            System.out.println(entry.getKey() + "---------" + entry.getValue());
        }

        System.out.println(scores.containsKey(new Student1("lisi", 2)));
        System.out.println(scores.containsValue("88"));

    }
}

class Student1 {
    private String name;
    private int stuNo;

    public Student1() {
    }

    public Student1(String name, int stuNo) {
        this.name = name;
        this.stuNo = stuNo;
    }

    public String getName() {
        return name;
    }

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

    public int getStuNo() {
        return stuNo;
    }

    public void setStuNo(int stuNo) {
        this.stuNo = stuNo;
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Student1 student1 = (Student1) o;
        return stuNo == student1.stuNo && Objects.equals(name, student1.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, stuNo);
    }
}
package com.qianfeng.collection;

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

/**
 * 功能描述
 *
 * @since 2022-05-16
 */
public class HashMapDemo2 {
    public static void main(String[] args) {
        Student1 student1 = new Student1("zhangsan", 1);
        Student1 student2 = new Student1("lisi", 2);
        Student1 student3 = new Student1("wangwu", 3);
        HashMap<Student1, Student1> scores = new HashMap<>();
        scores.put(student1, student1);
        scores.put(student2, student2);
        scores.put(student3, student3);
        System.out.println(scores);

        scores.remove(student1);

        for (Student1 score : scores.keySet()) {
            System.out.println(score + "========" + scores.get(score));
        }

        for (Map.Entry entry : scores.entrySet()) {
            System.out.println(entry.getKey() + "---------" + entry.getValue());
        }

        System.out.println(scores.containsKey(new Student1("lisi", 2)));
        System.out.println(scores.containsValue(new Student1("lisi", 2)));

    }
}