当前位置: 首页 > news >正文

网站建设主要包括前台和后台拼多多推广引流软件免费

网站建设主要包括前台和后台,拼多多推广引流软件免费,营销网讯,自助建网站工具HashSet HashSet是依靠hash table实现的(内部实现实际上是一个HashMap)。 不保证顺序(hash无法保证顺序)。 允许null值。 因为其实现借助于hash表,所以两个元素,e1.equals(e2),必须也要保证 e1.hashCode() e2.hashCode() LinkedHashSet LinkedHashSet继承自Hash…

HashSet

HashSet是依靠hash table实现的(内部实现实际上是一个HashMap)。 不保证顺序(hash无法保证顺序)。 允许null值。 因为其实现借助于hash表,所以两个元素,e1.equals(e2),必须也要保证 e1.hashCode() == e2.hashCode()

LinkedHashSet

LinkedHashSet继承自HashSet,不过和HashSet不同的是,它是借助于LinkedHashMap实现的(LinkedHashMap其实也继承自HashMap,但是它依靠双向链表,实现了插入顺序有序)。所以它的特性和HashSet类似,但是LinkedHashSet是有序的,因为有了链表,所以在遍历元素的时候LinkedHashSet要优于HashSet,但是在插入时,增加了维护链表的开销,所以插入性能略差于HashSet。

/** * 依靠Hash table和linked list实现的set集合,支持顺序。 * 和HashSet的区别是内部维护了一个双向链表,链表的顺序就是插入的顺序。 * 当一个元素重新插入set时,其内部顺序不受影响 */
public class LinkedHashSet<E>extends HashSet<E>implements Set<E>, Cloneable, java.io.Serializable

TreeSet

不同与HashSet的无序,LinkedHashSet的插入有序,TreeSet是有序的集合,其实现了SortedSet接口。TreeSet借助于TreeMap实现。Set内的元素按照 Comparable 或者 传入构造器的Comparator的顺序排序。TreeSet不支持null元素(因为要排序嘛)。

 

源码分析

HashSet

HashSet的构造函数初始化,实质上底层新建了一个HashMap的对象,可以参照之前的文章JDK8 HashMap源码详解 HashMap的构造函数对照着一起看。

/*** 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<>();}/*** Constructs a new set containing the elements in the specified* collection.  The <tt>HashMap</tt> is created with default load factor* (0.75) and an initial capacity sufficient to contain the elements in* the specified collection.** @param c the collection whose elements are to be placed into this set* @throws NullPointerException if the specified collection is null*/public HashSet(Collection<? extends E> c) {map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));addAll(c);}/*** Constructs a new, empty set; the backing <tt>HashMap</tt> instance has* the specified initial capacity and the specified load factor.** @param      initialCapacity   the initial capacity of the hash map* @param      loadFactor        the load factor of the hash map* @throws     IllegalArgumentException if the initial capacity is less*             than zero, or if the load factor is nonpositive*/public HashSet(int initialCapacity, float loadFactor) {map = new HashMap<>(initialCapacity, loadFactor);}/*** Constructs a new, empty set; the backing <tt>HashMap</tt> instance has* the specified initial capacity and default load factor (0.75).** @param      initialCapacity   the initial capacity of the hash table* @throws     IllegalArgumentException if the initial capacity is less*             than zero*/public HashSet(int initialCapacity) {map = new HashMap<>(initialCapacity);}/*** Constructs a new, empty linked hash set.  (This package private* constructor is only used by LinkedHashSet.) The backing* HashMap instance is a LinkedHashMap with the specified initial* capacity and the specified load factor.** @param      initialCapacity   the initial capacity of the hash map* @param      loadFactor        the load factor of the hash map* @param      dummy             ignored (distinguishes this*             constructor from other int, float constructor.)* @throws     IllegalArgumentException if the initial capacity is less*             than zero, or if the load factor is nonpositive*/HashSet(int initialCapacity, float loadFactor, boolean dummy) {map = new LinkedHashMap<>(initialCapacity, loadFactor);}

到这里,并不能明确HashSet传入的数据是用什么规则去插入到HashMap的,那么开始看看HashSet的add() 方法,这里有一个静态final参数PRESENT,按照源码中的释义是一个伪值,方便映射对象关联的。

 // Dummy value to associate with an Object in the backing Mapprivate static final Object PRESENT = new Object();

而这个伪值则用来在HashSet 新增时,作为底层HashMap的Value插入。到这里也不难理解为什么HashSet中的数据不能重复的原因了,存入的值,通过计算HashCode寻址,作为链表node 的属性key存入到HashMap 中去。

 /*** Adds the specified element to this set if it is not already present.* More formally, adds the specified element <tt>e</tt> to this set if* this set contains no element <tt>e2</tt> such that* <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.* If this set already contains the element, the call leaves the set* unchanged and returns <tt>false</tt>.** @param e element to be added to this set* @return <tt>true</tt> if this set did not already contain the specified* element*/public boolean add(E e) {return map.put(e, PRESENT)==null;}

其它的一些操作也都是和HashMap中的操作存在有对应的关系,也没太多好说的,可以将HashSet中的操作方法与HashMap源码中操作的方法对照起来看。

/*** Returns an iterator over the elements in this set.  The elements* are returned in no particular order.** @return an Iterator over the elements in this set* @see ConcurrentModificationException*/public Iterator<E> iterator() {return map.keySet().iterator();}/*** Returns the number of elements in this set (its cardinality).** @return the number of elements in this set (its cardinality)*/public int size() {return map.size();}/*** Returns <tt>true</tt> if this set contains no elements.** @return <tt>true</tt> if this set contains no elements*/public boolean isEmpty() {return map.isEmpty();}/*** Returns <tt>true</tt> if this set contains the specified element.* More formally, returns <tt>true</tt> if and only if this set* contains an element <tt>e</tt> such that* <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.** @param o element whose presence in this set is to be tested* @return <tt>true</tt> if this set contains the specified element*/public boolean contains(Object o) {return map.containsKey(o);}
/*** Removes the specified element from this set if it is present.* More formally, removes an element <tt>e</tt> such that* <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>,* if this set contains such an element.  Returns <tt>true</tt> if* this set contained the element (or equivalently, if this set* changed as a result of the call).  (This set will not contain the* element once the call returns.)** @param o object to be removed from this set, if present* @return <tt>true</tt> if the set contained the specified element*/public boolean remove(Object o) {return map.remove(o)==PRESENT;}/*** Removes all of the elements from this set.* The set will be empty after this call returns.*/public void clear() {map.clear();}

 

http://www.lbrq.cn/news/2609713.html

相关文章:

  • 什么叫 营销型网站网络营销带来的效果
  • 品牌网站建设搭建搜索引擎搜索器
  • 科技部火炬中心关键词优化报价推荐
  • 网站编辑合适内向的人做吗竞价代运营公司
  • 做电视的视频网站吗怎么申请域名建网站
  • 山东平台网站建设方案5151app是交友软件么
  • 肥西上派网站开发上海做seo的公司
  • 产品做优化好还是超级网站好太原网站建设谁家好
  • 怎么自己做网站卖东西重庆排名seo公司
  • wordpress 域名插件重庆公司网站seo
  • 成都中小企业网站建设建网站软件
  • 南宁定制网站建设网络营销的模式有哪些?
  • 政府网站建设公司西安关键词快速排名
  • 网站栏目及内容衡阳seo优化报价
  • 重庆建设厂招聘信息网站软文推荐
  • 芷江建设工程招投标网站中国最新消息新闻
  • 深圳专业集团网站建设百度指数什么意思
  • 做网站需要多少钱知乎百度图片识别在线使用
  • 长沙做网站 必看 磐石网络上海关键词优化排名软件
  • pbootcms快速仿站西安网站推广慧创科技
  • wordpress清理数据库的垃圾文件广州推动优化防控措施落地
  • 网站开发华企云商信息流投放
  • .net网站方案充电宝seo关键词优化
  • 合肥高端网站开发seo网络优化师就业前景
  • 如何做网站frontpage中国十大品牌营销策划公司
  • 怎么给网站加在线客服营业推广怎么写
  • 网站建设基础信阳seo优化
  • 住房城市建设委官方网站在线搜索资源
  • 如何做公司培训网站太原seo建站
  • 济南自适应网站建设营销策划案
  • 什么是RabbitMQ?
  • 前端保持和服务器时间同步的方法【使用vue3举例】
  • 力扣148:排序链表
  • 笔记学习杂记
  • 工作相关: 预刷真值与人工标注的真值之间的关系 以及 真值与原始数据的关系,
  • FreeRTOS源码分析四:时钟中断处理响应流程