当前位置:网站首页>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
边栏推荐
- Object.defineProperty() 和 数据代理
- Odoo project sends information to wechat official account or enterprise wechat
- Socket socket programming
- 如何在conda虚拟环境开启jupyter-notebook
- Capturing packets to find repeated acks and a large number of TCP retransmissions in TCP sessions -- sack (selective acknowledgement) technology
- The kernel fails to shut down when the easygbs program stops. How should I optimize it? [code attached]
- Fillet the tabbar with the flutter
- Copy image bitmap by C # memory method
- Google common syntax
- C Scrollview scroll up or scroll down
猜你喜欢

点云库pcl从入门到精通 第十章

给你的win10装一个wget

transform的结构及用法

Vulnhub | DC: 4 |【實戰】

数据资产为王,解析企业数字化转型与数据资产管理的关系

Implementation principle and source code analysis of ThreadPoolExecutor thread pool

谈谈 @Autowired 的实现原理

开源技术交流丨批流一体数据同步引擎ChunJun数据还原-DDL功能模块解析

After reading five books, I summarized these theories of wealth freedom

vtk. JS left mouse button sliding to change window level and window width
随机推荐
What are open source software, free software, copyleft and CC? Can't you tell them clearly?
After easynvr video is enabled, no video file is generated. How to solve this problem?
qt 不规则图形 消除锯齿
Active Directory之AD对象
Tencent cloud account related
How to solve the problem that flv video stream cannot be played and TS file generation fails due to packet loss?
Markdown学习
开源软件、自由软件、Copyleft、CC都是啥,傻傻分不清楚?
Apache Solr arbitrary file read replication
坑爹的“敬业福”:支付宝春晚红包技术大爆发
Vulnhub | dc: 4 | [actual combat]
[try to hack] IP address
十多年前的入职第一天
Set the time zone in ci4 (CodeIgniter 4)
Markdown learning
Tensorboard的使用
RTSP/ONVIF协议视频平台EasyNVR启动服务报错“service not found”,该如何解决?
A record of "from scratch" in college student accounts
The rtsp/onvif protocol video platform easynvr startup service reports an error "service not found". How to solve it?
Azure Active Directory brute force attack