首页 > 编程语言 >HashSet 源码解读

HashSet 源码解读

时间:2023-03-06 09:55:16浏览次数:31  
标签:set HashSet 解读 add 源码 new elements TreeSet

1.创建HashSet

Set<String> set = new HashSet<>();
set.add("aaa");

2.构造方法

private transient HashMap<E,Object> map;
/**
     * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
     * default initial capacity (16) and load factor (0.75).
     */
    public HashSet() {
        map = new HashMap<>();
    }

3.add方法 内部通过HashMap 保证数据不重复

 public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }

TreeSet

创建TreeSet

 Set<String> set = new TreeSet<>();
        set.add("aaa");

构造方法

private transient NavigableMap<E,Object> m;


Constructs a new, empty tree set, sorted according to the natural ordering of its elements. All elements inserted into the set must implement the Comparable interface.
Furthermore, all such elements must be mutually comparable: e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the set.
If the user attempts to add an element to the set that violates this constraint (for example, the user attempts to add a string element to a set whose elements are integers),
the add call will throw a ClassCastException.
public TreeSet() { this(new TreeMap<E,Object>()); }


/**
* Constructs a set backed by the specified navigable map.
*/
TreeSet(NavigableMap<E,Object> m) {
this.m = m;
}
 

add方法

 public boolean add(E e) {
        return m.put(e, PRESENT)==null;
    }

 

标签:set,HashSet,解读,add,源码,new,elements,TreeSet
From: https://www.cnblogs.com/wanglongjiang/p/17182729.html

相关文章

  • LinkedList 源码解读
    1.创建 LinkedListList<String>list=newLinkedList<>();list.add("wang");2.构造方法:开起了什么都没有做/***Constructsanemptylist.*/......
  • ArrayList源码解读
    1.创建ArrayListList<String>list=newArrayList<>();list.add("wang");2.构造方法:elementData的长度就是ArrayList的容量,在第一次使用时,elementData的长度会扩展......
  • nsq 源码剖析
    简要介绍主流MQ比较:目前主流的MQ有Kafka,RabbitMQ,NSQ,RocketMQ,ActiveMQ等,它们的对比如下:NSQ组件Topic:一个topic就是程序发布消息的一个逻辑键,......
  • OpenMP task construct 实现原理以及源码分析
    OpenMPtaskconstruct实现原理以及源码分析前言在本篇文章当中主要给大家介绍在OpenMP当中task的实现原理,以及他调用的相关的库函数的具体实现。在本篇文章当中最......
  • 【代理】【七】代理源码解析-Cglib代理-为什么Spring中的代理方法互相调用 AOP 会失效
    1 前言今天我们来看个问题,我看源码的时候产生的困惑,就是我们都知道SpringAOP我们方法中调用第二个方法,第二个方法不会走增强的逻辑,而原生的Cglib方法互相调用是会走......
  • MASA Framework源码解读-01 MASAFacotry工厂设计
    序言闲来无聊,前段时间发现一个.net开源框架:masaframework。经过一些小型项目使用,发现确实挺不错的。然后我又去阅读了整个masaframework源码,特此来记录整个源码阅读的......
  • 「死磕源码系列之ants协程池」高性能协程池ants源码剖析
    1、简介ants是什么ants是一个高性能的goroutine池,实现了对大规模goroutine的调度管理、goroutine复用,允许使用者在开发并发程序的时候限制goroutine数量,复用资源,达......
  • Django源码-startproject
    Django源码-startprojectDjango的所有命令都位于django/django/core/management/commands/目录下一命令目录django-adminstartprojectproject_name这个命令在源码里......
  • Django源码-测试和调试环境搭建
    Django源码-测试和调试环境搭建一创建虚拟环境mkvirtualenv-ppython3.8.10DjangoSource二安装Djangopipinstalldjango==4.0b1三下载源码方法一gitclon......
  • TCP通信聊天服务端和客户端(C/C++语言开发)附完整源码
    距离上次学Python写的Python实现简单聊天室已经过去好久了,现在学c++又写了一遍,其实过程差不多,无非是语法的变化,目前仅实现最简单的一对一的通信,然后改就是了,接下来应该是......