当前位置:网站首页>6.Map interface and implementation class
6.Map interface and implementation class
2022-08-09 09:31:00 【come here my bear】
Map interface and implementation class
Map system collection
graph TD;Interface.Map-->Class.HashMapInterface.Map-->Interface.SortedMapInterface.SortedMap-->Class.TreeMap
Interface: Interface
Class: Implementation class
Characteristics of the Map interface:
- Used to store arbitrary key-value pairs (Key-Value)
- Key: unordered, no subscript, no duplicates allowed (unique)
- values: unordered, no subscripts, duplicates allowed
Map parent interface
Features: store a pair of data (Key-Value), unordered, no subscripts, keys cannot be repeated, values can be repeated
Set(Map.Entry
) Set set with matching key value Collection values() returns a Collection containing all values
Method:
V put(K key, V value) Store the object in the collection and associate the key value.If the Key is repeated, the original value will be overwritten
Object get(Object key) Get the corresponding value according to the key
Set keySet(K) returns all Keys and returns a Key(key)
Set
> entrySet() Returns the Set collection containing the relationship in the collection and returns an Entry (key-value pair)
package com.jhkj.map;import java.util.HashMap;import java.util.Map;/*** Use of Map interface* Features:* 1. Stored as key-value pairs* 2. The key cannot be repeated, the value can be repeated* 3. Disorder*/public class Demo1 {public static void main(String[] args) {// create a Map objectMap map = new HashMap<>();// 1. Add datamap.put("cn","China");map.put("uk","UK");map.put("usa","United States");System.out.println("Number of elements: " + map.size());System.out.println(map.toString());// 2. Delete datamap.remove("usa");System.out.println(map.toString());// 3. Traverse the data// 3.1 Use keySet()System.out.println("----------use keySet()----------");for (String key : map.keySet()) {System.out.println(key + "-----" + map.get(key));}// 3.2 Use entrySet()System.out.println("----------use entrySet()------------");for (Map.Entry entry : map.entrySet()) {System.out.println(entry.getKey() + "------" + entry.getValue());}// 4. Judgment// Determine if the key existsSystem.out.println(map.containsKey("cn"));// Determine if Value existsSystem.out.println(map.containsValue("Russia"));}}
The implementation class of the Map collection
- HashMap (emphasis):
- Storage structure: hash table (array + linked list + red-black tree)
- JDK1.2 version, thread-unsafe, fast running efficiency; allow to use null as key or value
- Hashtable (learn about it):
- jdk1.0 version, thread-safe, allows slow efficiency; does not allow null as key or value
- Properties (subclass of Hashtable):
- A subclass of Hashtable that requires both key and value to be String.Usually used for reading configuration files
- TreeMap:
- Storage structure: red-black tree
- Implements the SortedMap interface (a sub-interface of Map), which can automatically sort keys
HashMap source code analysis summary
- When the HashMap is just created, the table is null. In order to save space, when the first element is added, the table capacity is adjusted to 16
- When the number of elements is greater than the threshold (16*0.75=12), the expansion will be performed, and the size will be twice the original size after expansion, and the number of adjusted elements will be reduced for the purpose
- Jdk1.8 When the length of each linked list is greater than 8 and the number of elements is greater than or equal to 64, it will be adjusted to a red-black tree to improve execution efficiency
- jdk1.8 When the length of the linked list is less than 6, it is adjusted to a linked list
- Before jdk1.8, the linked list was inserted at the head, and after jdk1.8, it was inserted at the end
The relationship between HashMap and HashSet
- Actually the HashMap class is called in the HashSet implementation class
public HashSet() {map = new HashMap<>();}
The add method calls the Key in the HashMap
public boolean add(E e) {return map.put(e, PRESENT)==null;}
Relationship between TreeSet and TreeMap
- In fact, the TreeMap class is called in the TreeSet implementation class
public TreeSet() {this(new TreeMap());}
边栏推荐
猜你喜欢
随机推荐
GBase数据库产生迁移工具假死的原因是什么?
What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?
LPP代码及其注释
Ontology development diary 02 - simple sparql query
“摄像头用不了”+win8.1+DELL+外置摄像头+USB免驱的解决办法
一个项目的整体测试流程有哪几个阶段?测试方法有哪些?
Consolidation of Questionnaire Questions and Answers
【面试体系知识点总结】---JVM
白盒测试的概念、目的是什么?及主要方法有哪些?
列表
使用Protege4和CO-ODE工具构建OWL本体的实用指南-1.3版本(4.Building An OWL Ontology)
Web请求原理
【机器学习】数据科学基础——机器学习基础实践(二)
A first look at the code to start, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, the first time to run the golang program EP01
接口测试主要测试哪方面?需要哪些技能?要怎么学习?
真·鸡汤文
static_assert报错为什么?
本体开发日记05-努力理解SWRL(下)
Lecture 4 SVN
seata处理分布式事务