当前位置:网站首页>Map interface and its sub implementation classes
Map interface and its sub implementation classes
2022-06-23 08:18:00 【Liang FuFu】
1. Map Interface
1.(1)Map<K,V> aggregate : An entity class that can describe things in the real world
(2) Objects that map keys to values , Represents a series of key value pairs
(3)Map Valid for key , Independent of value , Values can be repeated , But the key must be unique
2. Subclass implementation :
(1)HashMap: Implementation based on hash table Map Interface
characteristic : allow null Values and null Key exists , Thread unsafe class , There is no guarantee that the sequence of iterations will never change
(2)TreeMap: At the bottom is a red black tree structure
1.1 Map Basic functions of
1. To obtain the length of the :
public int size(): Get the number of collection elements
Add functionality :
public V put(K key,V value): Add key value pair elements , There is a return value : If key No repetition , The return value is null, Whether the return value is null Determine whether to add repeatedly . since Object Of hashCode() and equals() Method
Delete function :
public V remove(Object key): Delete specified key , And return the deleted value
public void clear(): Violent deletion , Clear all key value pair objects
Judgment function :
public boolean containsKey(Object key): Determine whether the specified key is included
public boolean containsValue(Object value): Determine whether to include the specified value
public boolean isEmpty(): Determines if the set is empty
1.2 Map The traversal of a set
1. Mode one : Get the set of all keys
public Set<K> keyset(): Get all the keys
public V get (Object key): Get the value of the key
2. Implementation code :
public class MapDemo{
public static void main(String[] args){
Map<String,String> map = new HashMap<>();
map.put(" Yang2 guo4 "," Little dragon female ");
map.put(" Guo Jing "," Huang Rong ");
map.put(" Erkang "," Crape myrtle ") ;
map.put(" Chen Xuanfeng "," Mei Chaofeng ") ;
map.put(" Jia Baoyu " ," Lin daiyu ") ;
// Get the set of all keys
Set<String> setKey = map.keySet();
// Get all keys , Traverse Set aggregate
for(String key:setKet){
String value = map.get(key);
sout(key+"\t"+value);
}
}
}
3. Mode two : Get all key value pair objects
Set<Map.Entry<K,V>> entrySet(); Get all key value pair objects , A key value pair object is called a mapping item
Use the methods in the interface :
K getKey(): Get key
V getValue(): Get value
4. Implementation code :
public class MapDemo{
public static void main(String[] args){
Map<String,String> map = new HashMap<>();
map.put(" Yang2 guo4 "," Little dragon female ") ;
map.put(" Guo Jing "," Huang Rong ") ;
map.put(" Erkang "," Crape myrtle ") ;
map.put(" Chen Xuanfeng "," Mei Chaofeng ") ;
map.put(" Jia Baoyu " ," Lin daiyu ") ;
map.put(" linghu chong " ," Ren Yingying ")
Set<Map.Entry<String,String>> entry = map.entrySet();
for(Map.Entry<String,String> en:entry){
String key = en.getKey();
String value = en.getValue();
sout(key+"\t"+value);
}
}
}
2.HashMap aggregate
1. If the key is jdk Type of offer , You don't have to rewrite it hashCode() and equals()
Custom types need to override both methods
3.TreeMap aggregate
Two ways of sorting :
1. Realization Comparabele Interface , Rewrite one of compareTo Method
2. Realization Comparator Interface , rewrite compare Method
边栏推荐
- Check the file through the port
- RTSP/ONVIF协议视频平台EasyNVR启动服务报错“service not found”,该如何解决?
- Set接口和Set子实现类
- Do not put files with garbled names into the CFS of NFS protocol
- 记一次高校学生账户的“从无到有”
- Socket programming -- select model
- 走好数据中台最后一公里,为什么说数据服务API是数据中台的标配?
- QT irregular shape antialiasing
- How to start Jupiter notebook in CONDA virtual environment
- There are some limitations in cluster expansion and contraction
猜你喜欢
随机推荐
生产环境服务器环境搭建+项目发布流程
如何在conda虚拟环境开启jupyter-notebook
Create an orderly sequence table and perform the following operations: 1 Insert element x into the table and keep it in order; 2. find the element with the value of X, and delete it if found; 3. outpu
Tencent cloud account related
Socket socket programming
aquatone工具 中的2個bug修複
Generate code 39 extension code in batch through Excel file
Integers and operators in go data types (3)
What are the PCB characteristics inspection items?
Go 数据类型篇(三)之整型及运算符
C# scrollView 向上滚动还是向下滚动
Cloud computing "half peak"
Sequence table Curriculum
7-调色板-CALayer和触摸
How to migrate x86 architecture applications to arm architecture at low cost
谈谈 @Autowired 的实现原理
Multi-scale feature combination in target detection
PHP serialization and deserialization CTF
复选框的基本使用与实现全选和反选功能
2 corrections de bogues dans l'outil aquatone





![Vulnhub | dc: 4 | [actual combat]](/img/33/b7422bdb18f39e9eb55855dbf1d584.png)



