当前位置:网站首页>Ensemble de cartes
Ensemble de cartes
2022-06-30 12:08:00 【Temps】
Table des matières
Un.、Caractéristiques
Désorganisation,Paire de clés,La clé ne peut pas être dupliquée,Les valeurs peuvent être répétées,Les touches dupliquées sont écrasées,Pas d'héritageCollectionInterface
2.、Expansion de la capacité
Expansion de la capacité:Capacité initiale16,Facteur de charge0.75,Augmentation de la capacité1X
Trois、Traversée
Préparation des données
private Map<String, Object>map=new HashMap<String, Object>();
@Before
public void setup() {
map.put("1", "Souris");
map.put("1", "Ordinateur");
map.put("2", "Ventilateur");
map.put("3", "Cartable");
map.put("4", "Discours");
map.putIfAbsent("1", "aaa");// Ajouter s'il manque
}
* Obtenez d'abord toutes les clés SetEnsemble,Encore une fois(Obtenir la valeur par clé)
@Test
public void test01() {
Iterator<String>it=map.keySet().iterator();
while(it.hasNext()) {
String key = it.next();
System.out.println(map.get(key));
}
}
L'image de l'effet est la suivante::
* Sortez et sauvegardez tout EntryDeSet, Encore une fois SetC'est tout.
@Test
public void test02() {
Iterator<Entry<String, Object>> it = map.entrySet().iterator();
while(it.hasNext()) {
Entry e=it.next();
System.out.println("key = "+ e.getKey() +" value ="+ e.getValue());
}
}L'image de l'effet est la suivante::
Quatre、Réalisation
HashMap
HashMapThread not Secure for, Mais le plus souvent ,Vite!. Les tableaux sont utilisés à l'interne pour stocker les données .
Commençons par comprendre Processus d'exécution
Quand on vamapÀ l'intérieurput Quand l'élément, Vérifiez d'abord si ce tableau existe . Si ce n'est pas le cas , Une longueur par défaut de 16Tableau de. Les éléments ne sont pas stockés directement dans le tableau , C'est une série d'opérations. , En donnant keyObtenirkeyPosition dans le tableau, Ce tableau s'appelle le seau ( En calculant le nombre de noeuds dans le baril ). L'élément pointé par le baril dans l'image est NodeNoeud(NodeEn fait,Entry<k,v>). Calculé chaque fois qu'un élément est ajouté ,S'il n'y a pas d'élément, Un noeud est déclaré dans , Si les éléments stockés sont les mêmes, ils seront écrasés ,Au contraire, Il est ajouté en descendant par une liste liée ( Les éléments ajoutés sont en haut , Les éléments originaux descendent ).
Comment obtenir la valeur ? D'abord calculé ,Adoptionkey Trouver le seau où se trouve l'élément , Si c'est une liste liée , Trouver l'élément en traversant la liste .

Avec beaucoup d'éléments , La méthode ci - dessus nécessite une traversée complète pour trouver ,Il y a des défauts. Recherche à l'aide d'un arbre binaire , Traversée par les conditions de l'élément , Cette liste n'a pas besoin d'être traversée .

Revenez après avoir compris la structure Justification
Tout d'abord,MapÀ l'intérieurputValeur, Si trouvé vide , Créer un nouvel espace ,Sur la basekeyDehush Indice correspondant dans le tableau de calcul de la valeur . S'il n'y a pas de valeur dans le tableau, un nouveau noeud est généré , Jugez les noeuds de construction s'il y a lieu ,Si plus de8, Convertir en arbre Rouge et noir pour enregistrer les informations du noeud ,Pas plus de8 Enregistrer l'information dans la liste liée .

Attention!: La partie verte de l'organigramme est JDK8 Nouvelle logique de traitement ,Le but est deTable[i]DansNodeNombre de noeuds supérieur à8Heure, Augmenter la vitesse de recherche grâce à l'arbre Rouge et noir .
HashTable
HashTable Thread Safe but not used
public void test03() {
Map<Integer, Object>table=new Hashtable<Integer, Object>();
table.put(1, "Zhang San");
table.put(2, "Li - si.");
table.put(3, "Wang Wu");
table.put(4, "Lao Liu");
Iterator<Integer> it = table.keySet().iterator();
while(it.hasNext()) {
int key=it.next();
System.out.println(table.get(key));
}
}ConcurrentHashMap
Sécurité des fils,QueHashTableHaute performance
public void test04() {
Map<Integer, Object>cmap=new ConcurrentHashMap<Integer, Object>();
cmap.put(1, "Zhang San");
cmap.put(2, "Li - si.");
cmap.put(3, "Wang Wu");
cmap.put(4, "Lao Liu");
Iterator<Integer> it = cmap.keySet().iterator();
while(it.hasNext()) {
int key=it.next();
System.out.println(cmap.get(key));
}
}TreeMap
key Les valeurs sont triées dans un certain ordre , Performances plus élevées lors de l'ajout ou de l'obtention d'éléments HashMapDoucement, C'est parce qu'il faut entretenir les arbres rouges et noirs à l'intérieur ,Pour garantirkeyOrdre des valeurs.
public void test05() {
Map<Integer, Object>tmap=new TreeMap<Integer, Object>();
tmap.put(1, "Zhang San");
tmap.put(2, "Li - si.");
tmap.put(3, "Wang Wu");
tmap.put(4, "Lao Liu");
Iterator<Integer> it = tmap.keySet().iterator();
while(it.hasNext()) {
int key=it.next();
System.out.println(tmap.get(key));
}
}key La valeur par défaut est ascendante , Si vous voulez trier par ordre décroissant, vous pouvez utiliser un comparateur (AveckeyComparer)
public void test05() {
Map<Integer, Object>tmap=new TreeMap<Integer, Object>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
tmap.put(1, "Zhang San");
tmap.put(2, "Li - si.");
tmap.put(3, "Wang Wu");
tmap.put(4, "Lao Liu");
Iterator<Integer> it = tmap.keySet().iterator();
while(it.hasNext()) {
int key=it.next();
System.out.println(tmap.get(key));
}
}L'image de l'effet est la suivante::

LinkedHashMap
SuccessionHashMap,LinkedHashMapC'est ordonné., Et par défaut à l'ordre d'insertion , Quand nous voulons stocker séquentiellement key-valueHeure,Il faut l'utiliser.LinkedHashMapC'est
public void test06() {
Map<String, String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("name1", "josan1");
linkedHashMap.put("name2", "josan2");
linkedHashMap.put("name3", "josan3");
Set<Entry<String, String>> set = linkedHashMap.entrySet();
Iterator<Entry<String, String>> iterator = set.iterator();
while(iterator.hasNext()) {
Entry entry = iterator.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
System.out.println("key:" + key + ",value:" + value);
}
}Les effets sont les suivants:

边栏推荐
- 21、wpf之绑定使用小记
- The website with id 0 that was requested wasn‘t found. Verify the website and try again
- Flutter 从零开始 007 输入框
- 1254. 统计封闭岛屿的数目
- YOLOv5导出onnx遇到的坑
- wallys/3×3 MIMO 802.11ac Mini PCIe Wi-Fi Module, QCA9880, 2,4GHz / 5GHzDesigned for Enterprise
- Object mapping - mapping Mapster
- Redis - SDS simple dynamic string
- How can c write an SQL parser
- NoSQL——Redis的配置与优化
猜你喜欢

zabbix监控TCP连接个数

YOLOv5导出onnx遇到的坑

Embedded sig | multi OS hybrid deployment framework

Conference Preview - Huawei 2012 lab global software technology summit - European session

Openmldb meetup No.4 meeting minutes

Hannaiping of Qilin software: the construction of Digital China needs its own open source root community

wallys/600VX – 2×2 MIMO 802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz QCA 9880

Our company has used this set of general solutions for 7 years, and has opened up dozens of systems, a stable batch!

wallys/IPQ8074a/2x(4 × 4 or 8 × 8) 11AX MU-MIMO DUAL CONCURRENT EMBEDDEDBOARD

redis在项目中的使用
随机推荐
Redis - ziplist compressed list
shell第一个命令结果传入第二个命令删除
WebView, Scrollview sliding conflict correction
Using cookie technology to realize historical browsing records and control the number of displays
Redis - SDS simple dynamic string
Flutter 从零开始 006 单选开关和复选框
Another miserable day by kotlin grammar
用宝塔建第2个网站时网站总是报错:No input file specified.
R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的colour参数指定不同分组的数据点使用不同的颜色显示
【云原生 | Kubernetes篇】深入了解Deployment(八)
R语言ggplot2可视化:使用ggplot2可视化散点图、在geom_point参数中设置alpha参数指定数据点的透明度级别(points transparent、从0到1)
led背光板的作用是什么呢?
R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram and use scale_ color_ viridis_ D function specifies the color scheme of data points
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
edusoho企培版纯内网部署教程(解决播放器,上传,后台卡顿问题)
Our company has used this set of general solutions for 7 years, and has opened up dozens of systems, a stable batch!
nvm安装node后,在使用npm指令时候显示不是内部或外部指令
redis在项目中的使用
1020. number of enclaves
R语言ggplot2可视化:使用ggplot2可视化散点图、aes函数中的size参数指定数据点的大小(point size)