当前位置:网站首页>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
边栏推荐
- 点云库pcl从入门到精通 第十章
- Go 数据类型篇(三)之整型及运算符
- APM performance monitoring practice of jubasha app
- Captain Abu's soul torture
- Imperva- method of finding regular match timeout
- What are open source software, free software, copyleft and CC? Can't you tell them clearly?
- 训练后的随机森林模型导出和加载
- 实战监听Eureka client的缓存更新
- 2 corrections de bogues dans l'outil aquatone
- MFC Radio Button分组
猜你喜欢

值得反复回味的81句高人高语

建立一有序的顺序表,并实现下列操作: 1.把元素x插入表中并保持有序; 2.查找值为x的元素,若找到将其删除; 3.输出表中各元素的值。

VTK. Le bouton gauche de la souris JS glisse pour changer le niveau et la largeur de la fenêtre

图像分割-改进网络结构

Vulnhub | DC: 4 |【实战】

Socket programming (multi process)

81 sentences worth repeating

Vulnhub | DC: 3 |【实战】

jmeter压测结果分析
![Vulnhub | dc: 3 | [actual combat]](/img/97/e5ba86f2694fe1705c13c60484cff6.png)
Vulnhub | dc: 3 | [actual combat]
随机推荐
View the file once a second and send the result of the last line of the file to the syslog server
Captain Abu's soul torture
kibana 重建index后,如何恢复Visualizations和 Dashboards
Socket programming -- select model
开源技术交流丨批流一体数据同步引擎ChunJun数据还原-DDL功能模块解析
Quickly create a consumer cluster
MySQL小册子笔记 5 InnoDB 记录存储结构
值得反复回味的81句高人高语
点云库pcl从入门到精通 第十章
开源软件、自由软件、Copyleft、CC都是啥,傻傻分不清楚?
目标检测中的多尺度特征结合方式
坑爹的“敬业福”:支付宝春晚红包技术大爆发
Active Directory之AD对象
[try to hack] IP address
Deep learning ----- convolution (conv2d) bottom layer
Lightweight UI control library worth collecting
Focus! Ten minutes to master Newton convex optimization
What are the PCB characteristics inspection items?
PCB电路板特性检查项目都有哪些?
3-ProgressBar和二次裁剪