当前位置:网站首页>Map collection traversal, adding, replacing and deleting elements
Map collection traversal, adding, replacing and deleting elements
2022-06-21 13:25:00 【zhangsan3333】
- map It will be saved in the order of storage value
- Access can only be based on key obtain value, Cannot pass value obtain key.
Add elements :
public V put(K key, V value): Add the specified key and specified value to Map Collection .explain :
In the use of put Store a pair of elements (key-value) Object time , Will take it first key To judge Map Whether the collection already exists .
If Map There are no identical in the set key There is : Just put key-value Store in Map Collection , And back to null value .
If Map There are the same in the set key There is : Will put the previously stored value Object to cover . And return to the previous value object ( used value object ).
This can be understood as modifying value, But it can't be modified key.
Be careful : because Map It's the interface , Cannot create object , Only use Map The following subclasses HashMap Create objects .
Remove elements :
public V remove(Object key): Put the specified key The corresponding key value pair element stay Map Delete... From the collection , Returns the value of the deleted element .
remove It's based on key Delete the current key Corresponding value This set of data :If Map The set contains the specified key The same elements exist : Delete a pair key-value object , Instead of just deleting value, And return to delete this group value value ;
If Map There is no relation to the specified in the collection key The same elements exist : No delete operation , return null;
problem : Why not through value To delete a set of data ?
because key Is the only one. , and value There can be multiple , So we can't go through value To delete a set of data , Only through key Value to delete .
Empty the set .void clear()
package MapTest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Map12 {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "0000049259131");
map.put(2, "0000049363609");
map.put(3, "0000049363685");
map.put(4, "0000049363654");
map.put(5, "0019428006716");
map.put(6, "0010343864245");
map.put(7, "0010343865709");
map.put(8, "0010343865716");
map.put(9, "0010343865723");
map.put(10, "0010343865730");
map.put(11, "0010343865747");
map.put(12, "0010343865754");
map.put(13, "0010343865761");
map.put(14, "0010942205456");
map.put(15, "0010942206545");
map.put(16, "0010942206613");
map.put(17, "0010942206699");
map.put(18, "0010942207528");
//put Method actually has a return value , yes V type
String put1 = map.put(19, "0010942207542");
//map The collection has no duplicates key, return null, Deposit in key-value
String put2 = map.put(20, "2022032600003");
//map Collection has duplicate key, Go back to the old value, Overwrite old value, Replace with new value
String put3 = map.put(20, "2022032600001");
System.out.println("put1 = " + put1);
System.out.println("put2 = " + put2);
System.out.println(" used value = " + put3);
System.out.println("map = " + map);
String remove = map.remove(20);
System.out.println(" Remove elements = " + remove);
// Re insert the element
map.put(20, "2022032600002");
// By traversing keySet, obtain value
Set<Integer> keys = map.keySet();
for (Integer key : keys) {
String value = map.get(key);
System.out.println(key + "------" + value);
}
map.clear();
System.out.println(" After emptying map = " + map);
// Print nothing
for (Integer key : keys) {
String value = map.get(key);
System.out.println(key + "------" + value);
System.out.println(" Yes ...");
}
System.out.println(" end ");
}
}

边栏推荐
- Graveyard
- Eureka的TimedSupervisorTask类(自动调节间隔的周期性任务)
- Not only zero:bmtrain technology principle analysis
- 使用seaborn绘制热图
- Apache shardingsphere 5.1.2 release | new driving API + cloud native deployment to create a high-performance data gateway
- Convert DICOM format to nii GZ file
- 《預訓練周刊》第50期:無解碼變換器、神經提示搜索、梯度空間降維
- 2. reference
- Nanjing University static program analyses -- Introduction learning notes
- PingCAP 入选 2022 Gartner 云数据库“客户之声”,获评“卓越表现者”最高分
猜你喜欢

Kubernetes快速实战与核心原理剖析

《預訓練周刊》第50期:無解碼變換器、神經提示搜索、梯度空間降維

Highly available configuration of database (MySQL)

Heat mapping using Seaborn
![[deeply understand tcapulusdb technology] tcapulusdb import data](/img/bd/999a0d2020f68b3bcee6b617328dfc.png)
[deeply understand tcapulusdb technology] tcapulusdb import data

服务治理的工作内容

hands-on-data-analysis 第二单元 第四节数据可视化
![[course assignment] floating point operation analysis and precision improvement](/img/de/ae3c1a41b81dffd8e4299bbe926099.png)
[course assignment] floating point operation analysis and precision improvement

爱可可AI前沿推介(6.21)

3D slicer import label and view label
随机推荐
Application configuration management, basic principle analysis
Nanjing University static program analyses -- Introduction learning notes
Distributed transaction processing scheme big PK
Reading notes on how to connect the Internet ADSL
[deeply understand tcapulusdb technology] tmonitor background one click installation
Display all indexes of a table in Oracle
uva11292
5. array
如何编写测试用例
[deeply understand tcapulusdb technology] tmonitor system upgrade
对app和微信小程序进行接口测试
[upgraded student information management system] + file operation + more details
6. functions
Deep understanding of convolution in convolution neural network
The new plan for national treasures - the exclusive digital collection of the four museums is coming!
Qinglong panel, JD timed task library, script library
2. data type
Comment utiliser un moteur de recherche?
What is Devops in an article?
TOOD: Task-aligned One-stage Object Detection