【Java】HashSet集合用法

目录

[HashSet 集合特点](#HashSet 集合特点)

示例代码

手写HashSet集合

[HashSet 没有Get()](#HashSet 没有Get())


HashSet 集合特点

  1. HashSet 基于HashMap 来实现的,是一个不允许有重复元素的集合
  2. HashSet 允许有 null 值
  3. HashSet 是无序的,即不会记录插入的顺序
  4. HashSet集合实现了Set接口
  5. HashSet 没有Get(),所以不能使用普通for循环遍历

示例代码

java 复制代码
package com.collection.Demo09;

import java.util.HashSet;
import java.util.Iterator;

public class Test02 {
    public static void main(String[] args) {
        HashSet<String> strings = new HashSet<>();
        strings.add("mayilt01");
        strings.add("mayilt02");
        strings.add("mayilt03");
        strings.add("mayilt03"); //不允许有重复值,∵底层基于HashMap集合实现,
        //而HashSet.add() 底层存放元素 采用HashMap集合 Key 来存放,HashMap key值是不允许重复
        /**
         *     public boolean add(E e) {
         *         return map.put(e, PRESENT)==null;
         *     }
         */
        strings.add(null);//HashMap允许存放Key值 是为null
        Iterator<String> iterator = strings.iterator();
        while (iterator.hasNext()){
            System.out.print(iterator.next());//null,mayilt01,mayilt03,mayilt02,
            System.out.print(","); //上面输出结果说明:HashSet是无序的,∵底层基于HashMap集合实现
        }
    }
}

手写HashSet集合

java 复制代码
package com.collection.Demo09;

import java.util.HashMap;

/**
 * 手写HashSet集合
 */
public class MayiktHashSet<E> {
    /**
     * HashSet 底层是基于 HashMap 集合实现
     * 元素的值 就是为 HashMap 中的key
     */
    private HashMap<E, Object> map;
    private static final Object PRESENT = new Object();//小写转大写,快捷键 Ctrl+Shift+u

    public MayiktHashSet() {
        map = new HashMap<>();
    }

    public void add(E e) {
        map.put(e, PRESENT);
    }

    @Override
    public String toString() {
        return "MayiktHashSet{" +
                "map=" + map +
                '}';
    }

    public static void main(String[] args) {
        MayiktHashSet<Object> hashSet = new MayiktHashSet<>();
        hashSet.add("mayikt01");
        hashSet.add("mayikt02");
        hashSet.add("mayikt03");
        hashSet.add("mayikt03");//不允许重复值,下面输出中只有一个mayikt03
        System.out.println(hashSet);
        //MayiktHashSet{map={mayikt03=java.lang.Object@4554617c,
        // mayikt01=java.lang.Object@4554617c,
        // mayikt02=java.lang.Object@4554617c}}
    }

}

HashSet 没有Get()

java 复制代码
package com.collection.Demo09;

import java.util.HashSet;

public class Test03 {
    public static void main(String[] args) {
        HashSet<String> strings = new HashSet<>();
        strings.add("mayikt01");
        strings.add("mayikt02");
        strings.add("mayikt03");
        //HashSet 没有get(), ∵底层基于HashMap实现的,HashMap 存放Key是散列的,就没有使用index访问元素
        //∴不能够使用普通的for循环
//        for (int i = 0; i < strings.size(); i++) {}
        for (String str : strings) {
            System.out.println(str);
        }
    }
}

下一篇文章:HashSet存入学生对象

相关推荐
考虑考虑17 小时前
Jpa使用union all
java·spring boot·后端
用户37215742613518 小时前
Java 实现 Excel 与 TXT 文本高效互转
java
浮游本尊19 小时前
Java学习第22天 - 云原生与容器化
java
渣哥20 小时前
原来 Java 里线程安全集合有这么多种
java
间彧21 小时前
Spring Boot集成Spring Security完整指南
java
间彧21 小时前
Spring Secutiy基本原理及工作流程
java
Java水解1 天前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
洛小豆1 天前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试
前端小张同学1 天前
服务器上如何搭建jenkins 服务CI/CD😎😎
java·后端
ytadpole1 天前
Spring Cloud Gateway:一次不规范 URL 引发的路由转发404问题排查
java·后端