欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2021-01-07 HashSet

程序员文章站 2022-03-15 20:53:38
...
package com.llb.hashSet;

import java.util.Objects;

/**
 * 哈希值
 *   是JDK根据对象的地址值或者属性值,算出来的int类型的整数
 *     hashCode()获取哈希值
 *   特点
 *     如果没有重写hashCode()方法,那么是根据对象的地址值计算出的哈希值
 *     如果重写了hashCode()方法,一般都是通过对象的属性值计算出哈希值
 *   JDK7
 *     底层结构  哈希表(数组+链表)
 *       1,创建一个默认长度16,默认加载因子0.75的数组(16*0.17=12的时候扩容,扩容为原来的2倍),数组名table
 *     添加元素
 *       2,根据元素的哈希值跟数组的长度计算出存入的位置
 *       3,判断当前位置是否为null,如果是null直接存入
 *       4,如果存入的位置不为null,表示有元素,则调用equals方法比较属性值
 *       5,如果一样则不存,不一样则存入数组,新元素存入表中,老元素挂在新元素下面,和新元素形成一个链表
 *   JDK8
 *     底层结构  数组+红黑树
 *       当链表长度超过8,则将链表换成红黑树
 *       步骤
 *         计算哈希值
 *         计算应存入的索引
 *         是否为空
 *           为空,直接存
 *           不为空,判断结构
 *             链表
 *             红黑树
 */
public class MyHashSet {
    public static void main(String[] args) {
        Student hinata = new Student("hinata", 18);
        Student naruto = new Student("naruto", 18);
        //object类中  根据对象的地址值计算出来的哈希值
        System.out.println(hinata.hashCode());//356573597  重写后921755666
        System.out.println(naruto.hashCode());//1735600054 重写后1727067586

    }
}
class Student{
    private String name;
    private Integer age;

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

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

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

    public Student() {
    }
}



package com.llb.hashSet;

import java.util.HashSet;
import java.util.Objects;

public class MyHashSetExe {
    public static void main(String[] args) {
        //自定义对象要存入hashSet集合中,需要重写hashCode()和equals()方法
        HashSet<Studen00t> hashSet = new HashSet<>();
        Studen00t aaa = new Studen00t("aaa", 18);
        Studen00t bbb = new Studen00t("bbb", 18);
        Studen00t ccc = new Studen00t("ccc", 18);
        Studen00t ddd = new Studen00t("ccc", 18);
        hashSet.add(aaa);
        hashSet.add(bbb);
        hashSet.add(ccc);
        hashSet.add(ddd);
        for (Studen00t studen00t : hashSet) {
            System.out.println(studen00t);
        }
    }
}
class Studen00t{
    private String name;
    private Integer age;

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

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Studen00t(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Studen00t() {
    }
}

相关标签: hashcode