Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Set Source Code Analysis of JDK

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "Set source code analysis of JDK". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope that this article "Set source code analysis of JDK" can help you solve the problem.

First, let's take a look at HashSet

Private transient HashMap map

/ / Dummy value to associate with an Object in the backing Map

Private static final Object PRESENT = new Object ()

It can be seen that he adapts to HashMap, so how is his function delegated to the HashMap structure?

Public boolean add (E e) {

Return map.put (e, PRESENT) = = null

}

In hashMap, we use value most of the time, but in set, we make good use of the existing class HashMap, which makes use of the uniqueness of HashMap's key to ensure the uniqueness of the elements stored in Set.

Private static final Object PRESENT = new Object ()

It's the value of all the key of this HashMap, it's just a form, and our real data is the resources that exist in the key.

The last iterator we got was also:

Public Iterator iterator () {

Return map.keySet () .iterator ()

}

Iterator of map's keySet

Similarly, let's take a look at LinkedhashMap

Public LinkedHashSet (int initialCapacity, float loadFactor) {

Super (initialCapacity, loadFactor, true)

}

/ * *

* Constructs a new, empty linked hash set with the specified initial

* capacity and the default load factor (0.75)

*

* @ param initialCapacity the initial capacity of the LinkedHashSet

* @ throws IllegalArgumentException if the initial capacity is less

* than zero

, /

Public LinkedHashSet (int initialCapacity) {

Super (initialCapacity, .75f, true)

}

/ * *

* Constructs a new, empty linked hash set with the default initial

* capacity (16) and load factor (0.75)

, /

Public LinkedHashSet () {

Super (16, .75f, true)

}

The constructor of the parent class is called; the constructor is as follows:

HashSet (int initialCapacity, float loadFactor, boolean dummy) {

Map = new LinkedHashMap (initialCapacity, loadFactor)

}

Gave birth to LinkedHashMap.

Similarly, we can also see the implementation of treeMap:

Private transient NavigableMap m

/ / Dummy value to associate with an Object in the backing Map

Private static final Object PRESENT = new Object ()

This is the end of the content of "Set source code analysis of JDK". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report