当前位置:网站首页>Map集合遍历,添加替换,删除元素
Map集合遍历,添加替换,删除元素
2022-06-21 10:50:00 【zhangsan3333】
- map会按照存储的顺序保存value
- 获取也只能根据key获取value,不能通过value获取key。
增加元素:
public V put(K key, V value): 把指定的键与指定的值添加到Map集合中。说明:
在使用put存储一对元素(key-value)对象时,会先拿key去判断Map集合中是否已经存在。
如果Map集合中没有相同的key存在:就把key-value存储到Map集合中,并返回null值。
如果Map集合中有相同的key存在:会把之前存储的value对象覆盖。并返回之前的value对象(旧value对象)。
这里可以理解为修改value,但是不能修改key。
注意:由于Map是接口,不能创建对象,只能使用Map下面的子类HashMap创建对象。
删除元素:
public V remove(Object key): 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值。
remove是根据key删除当前key对应的value这一组数据:如果Map集合中有与指定的key相同的元素存在:则删除一对key-value对象,而不是只删除value,并返回删除这组的value值;
如果Map集合中没有与指定的key相同的元素存在:没有删除操作,返回null;
问题:为什么不可以通过value来删除一组数据?
因为key是唯一的,而value可以有多个,所以不能通过value来删除一组数据,只能通过key值进行删除。
清空集合。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方法其实有返回值,是V类型
String put1 = map.put(19, "0010942207542");
//map集合没有重复的key,返回null,存入key-value
String put2 = map.put(20, "2022032600003");
//map集合有重复的key,返回旧value,覆盖旧value,替换为新value
String put3 = map.put(20, "2022032600001");
System.out.println("put1 = " + put1);
System.out.println("put2 = " + put2);
System.out.println("旧value = " + put3);
System.out.println("map = " + map);
String remove = map.remove(20);
System.out.println("移除元素 = " + remove);
//重新放入元素
map.put(20, "2022032600002");
//通过遍历keySet,获取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("清空后map = " + map);
//什么都不打印
for (Integer key : keys) {
String value = map.get(key);
System.out.println(key + "------" + value);
System.out.println("执行了。。。");
}
System.out.println("结束");
}
}

边栏推荐
- MySQL - data type
- Financial institutions scramble for "digital employees"; Release of aging service evaluation framework for insurance app
- The delimiter connects the list string without secondary processing joiner on(). join()&&String. join(“,“, list)
- Brief introduction of quality control conditions before genotype filling
- 05. Redis core chapter: the secret that can only be broken quickly
- The "first city" in Central China. How can Changsha be built?
- A bit of knowledge - what is amp?
- Mqtt of NLog custom target
- Eig and Saudi Aramco signed a memorandum of understanding to expand energy cooperation
- Mythical Games宣布与韩国领先游戏发行商Kakao Games合作,推动亚太区业务扩张
猜你喜欢
随机推荐
Black Monday
Handling of legal instruction problems
Unable to access gcr IO solutions
中部“第一城”,网安长沙以何安网?
Optimisation des performances - compression, chargement et formatage des images
程序員新人周一優化一行代碼,周三被勸退?
Why does C throw exceptions when accessing null fields?
实测:云RDS MySQL性能是自建的1.6倍
知识点滴 - 什么是加速移动网页(AMP)?
2022年最强八股文《码出八股文-斩出offer线》
Dapr advanced-01-debug dapr
cvte一面
04. New features of redis: Interpretation of multithreading model
Intel和台积电的先进工艺阻力越来越大,中国芯片有望缩短差距
一行代码加速 sklearn 运算上千倍
Start from scratch 10- background management system development
Application configuration management, basic principle analysis
Is it safe for Guojin securities to open an account?
Es composite query workload evaluation
MySQL 5.7都即将停只维护了,是时候学习一波MySQL 8了








