|NO.Z.00059|——————————|BigDataEnd|——|Java&集合类库.V07|——|Java.v07|HashSet集合|

一、HashSet集合的编程使用
### --- 常用的方法

——>        参考Collection集合中的方法即可!
### --- 案例题目

——>        准备一个Set集合指向HashSet对象,向该集合中添加元素"two"并打印,
——>        再向集合中添加元素"one"并打印,再向集合中添加元素"three"并打印,
——>        再向集合中添加"one"并打印。
### --- 元素放入HashSet集合的原理

——>        使用元素调用hashCode方法获取对应的哈希码值,
——>        再由某种哈希算法计算出该元素在数组中的索引位置。
——>        若该位置没有元素,则将该元素直接放入即可。
——>        若该位置有元素,则使用新元素与已有元素依次比较哈希值,
——>        若哈希值不相同,则将该元素直接放入。
——>        若新元素与已有元素的哈希值相同,则使用新元素调用equals方法与已有元素依次比较。
——>        若相等则添加元素失败,否则将元素直接放入即可。
——>        思考:为什么要求重写equals方法后要重写hashCode方法呢?
### --- 解析:

——>        当两个元素调用equals方法相等时证明这两个元素相同,
——>        重写hashCode方法后保证这两个元素得到的哈希码值相同,
——>        由同一个哈希算法生成的索引位置相同,
——>        此时只需要与该索引位置已有元素比较即可,从而提高效率并避免重复元素的出现。
二、元素放入哈希表的过程
三、编程使用
package com.yanqi.task15;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

public class HashSetTest {

    public static void main(String[] args) {

        // 1.声明一个Set类型的引用指向HashSet类型的对象
        Set<String> s1 = new HashSet<>();
        //Set<String> s1 = new LinkedHashSet<>();  // 将放入的元素使用双链表连接起来
        System.out.println("s1 = " + s1); // [啥也没有]

        System.out.println("----------------------------------------------------");
        // 2.向集合中添加元素并打印
        boolean b1 = s1.add("two");
        System.out.println("b1 = " + b1); // true
        System.out.println("s1 = " + s1); // [two]
        // 从打印结果上可以看到元素没有先后放入次序(表面)
        b1 = s1.add("one");
        System.out.println("b1 = " + b1); // true
        System.out.println("s1 = " + s1); // [one, two]   [two, one]

        b1 = s1.add("three");
        System.out.println("b1 = " + b1); // true
        System.out.println("s1 = " + s1); // [one, two, three]  [two, one, three]
        // 验证元素不能重复
        b1 = s1.add("one");
        System.out.println("b1 = " + b1); // false
        System.out.println("s1 = " + s1); // [one, two, three] [two, one, three]
    }
}
四、编程打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=53231:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task15.HashSetTest
s1 = []
----------------------------------------------------
b1 = true
s1 = [two]
b1 = true
s1 = [one, two]
b1 = true
s1 = [one, two, three]
b1 = false
s1 = [one, two, three]

Process finished with exit code 0

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(20)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示