当前位置:网站首页>2022-08-07 The fifth group Gu Xiangquan study notes day31-collection-Map collection
2022-08-07 The fifth group Gu Xiangquan study notes day31-collection-Map collection
2022-08-08 14:02:00 【aggressive leeks】
集合
学习内容
Map集合
MapThe collections in all store elements in key-value pairs
Map集合继承关系
Map接口
- 包路径:java.util.Map<K,V>
- 方法
MapThe key method in the interface
方法 | 功能 |
---|---|
boolean containsKey(Object) boolean containsValue(Object) V remove(Object key) | when using these three methods,The type stored in the collection is required to be重写equals() |
Set<K> keySet() | 返回此映射中包含的键的 Set 视图.该 set 受映射支持1,So changes to the mapping can be made here set 中反映出来,反之亦然.如果对该 set The map is modified while iterating(Pass the iterator's own remove 操作除外),Then the iteration result is indeterminate.set 支持元素移除,通过 Iterator.remove、Set.remove、removeAll、retainAll 和 clear Actions remove the corresponding mapping from the map.它不支持 add 或 addAll 操作 上面时JDK帮助文档的原话,简单来说就是,Map集合调用keySet()时会返回一个Set集合,对这个SetCollection modifications will cause MapThe contents of the collection are modified together,反之对MapCollection modifications can also resultSetModify the contents of the collection together;并且对Set集合进行遍历时,支持删除元素(remove),不支持添加元素(add或addAll),And use pairs of iteratorsSetIterators must be used when traversing the collectionremove()删除元素,Otherwise the iteration result is undefined |
Collection values() | 返回此映射中包含的值的 Collection 视图.该 collection 受映射支持,So changes to the mapping can be made here collection 中反映出来,反之亦然.如果对该 collection The map is modified while iterating(Pass the iterator's own remove 操作除外),Then the iteration result is indeterminate.collection 支持元素移除,通过 Iterator.remove、Collection.remove、removeAll、retainAll 和 clear Actions remove the corresponding mapping from the map.它不支持 add 或 addAll 操作. |
Set<Map.Entry<K,V>> entrySet() | 返回此映射中包含的映射关系的 Set 视图.该 set 受映射支持,So changes to the mapping can be made here set 中反映出来,反之亦然.如果对该 set The map is modified while iterating(Pass the iterator's own remove 操作,Or by doing it on the map item returned by the iterator setValue 操作除外),Then the iteration result is indeterminate.set 支持元素移除,通过 Iterator.remove、Set.remove、removeAll、retainAll 和 clear Actions remove the corresponding mapping from the map.它不支持 add 或 addAll 操作. |
Map集合的遍历/迭代
Map集合不能直接遍历,可以通过遍历Mapping-backedSet集合/Collection集合to traverse indirectlyMap集合
- 遍历keySet()返回的Set集合
package com.jsoft.collection;
import java.util.*;
public class TestMap {
public static void main(String[] args) {
TreeMap<String, String> stringTreeMap = new TreeMap<>();
stringTreeMap.put("1","20");
stringTreeMap.put("5","120");
stringTreeMap.put("20","250");
stringTreeMap.put("1","22");
stringTreeMap.put("12","20");
stringTreeMap.put("A","260");
stringTreeMap.put("z","720");
// 遍历keySet()返回的Set集合
Set<String> keySet = stringTreeMap.keySet();
for (String key : keySet) {
// 通过key拿到value
String value = stringTreeMap.get(key);
// print to the consoleMap集合的key和value
System.out.println(key + "=" + value);
}
}
}
- 遍历entrySet()返回的Set集合
package com.jsoft.collection;
import java.util.*;
public class TestMap {
public static void main(String[] args) {
TreeMap<String, String> stringTreeMap = new TreeMap<>();
stringTreeMap.put("1","20");
stringTreeMap.put("5","120");
stringTreeMap.put("20","250");
stringTreeMap.put("1","22");
stringTreeMap.put("12","20");
stringTreeMap.put("A","260");
stringTreeMap.put("z","720");
// 遍历entrySet()返回的Set集合
Set<Map.Entry<String, String>> entrySet = stringTreeMap.entrySet();
for(Map.Entry<String,String> entry : entrySet) {
// 通过Map.Entry拿到key
String key = entry.getKey();
// 通过Map.Entry拿到value
String value = entry.getValue();
// 向控制台打印Map集合key和value
System.out.println(key + "=" + value);
}
}
}
- 遍历values()返回的Collection集合
package com.jsoft.collection;
import java.util.*;
public class TestMap {
public static void main(String[] args) {
TreeMap<String, String> stringTreeMap = new TreeMap<>();
stringTreeMap.put("1","20");
stringTreeMap.put("5","120");
stringTreeMap.put("20","250");
stringTreeMap.put("1","22");
stringTreeMap.put("12","20");
stringTreeMap.put("A","260");
stringTreeMap.put("z","720");
// 遍历values()返回的Collection集合,只能获取Map集合的key部分
Collection<String> values = stringTreeMap.values();
for(String key : values) {
System.out.println("key: " + key);
}
}
}
对于方式2和方式3来遍历Map集合的key和value,建议使用方式3,因为方式3可以直接从entry对象中获取属性值,效率高;方式2需要通过key去获取value(挨个遍历),效率低
HashMap类
- HashMap集合底层采用了哈希表这种数据结构
Hashtable类
- Hashtable集合底层采用了哈希表这种数据结构
- Hashtable集合和HashMap集合区别 ----> Hashtable是线程安全的(Hashtable中的方法都有synchronized关键字修饰),There is now a better way to ensure thread safety,Hashtable已过时
Properties类
- Properties集合继承了Hashtable集合,所以Properties也是线程安全的
- Properties集合中的key和value部分只能是String类型
- Properties又被称为属性类
SortedMap接口
- SortedMap接口继承了Map接口的特点(无序不可重复)
- SortedMapThe elements in the collection are all sortable(根据keyParts are automatically sorted)
TreeMap类
- TreeMap接口继承了SortedMap接口的特点,The elements in the collection are based onkeyParts are automatically sorted
TreeSet集合和TreeMapThe type of storage in the collection必须实现Comparable接口,Otherwise it will appear while the program is runningClassCaseException.
UML类图
对映射(Map集合)Changes can be made at Set中反映出来,反之亦然(即对SetChanges to collections can also be reflected in the map) ︎
边栏推荐
猜你喜欢
暗恋云匹配匿名交友聊天系统开发
Tsinghua | GLM-130B: An Open Bilingual Pre-training Model
HackTheBox | Horizontall
Full of dry goods, Yu Jingxin class of the Institute of Information Technology, Chinese Academy of Sciences will help you get academic research and thesis writing skills
Review: What is the pre-approval of autumn recruitment?What is an ordinary autumn move?It's all recruitment, why do you need to set these two recruitment time periods?
shell三剑客-----sed命令
HackTheBox | Horizontall
小白大白读论文-关于EfficientNetV2论文的 疑问 与 总结
KD-SCFNet:通过知识蒸馏实现更准确、更高效的显着目标检测(ECCV2022)
「复盘」面试BAMT回来整理398道高频面试题,助你拿高薪offer
随机推荐
TS+Hooks二次封装antd Modal,实现可拖拽
leetcode 155. Min Stack最小栈(中等)
MySQL:索引(1)原理与底层结构
干货满满,中科院信工所于静新课帮你get学术研究与论文写作技能
【Rust—LeetCode题解】1408.数组中的字符串匹配
南非 KMP 媒体集团实施了 DMS(文档管理系统)使流程数字化,员工可以再次专注于他们的实际任务,提供了效率
idea增加左右箭头
R语言使用位置索引筛选dataframe的数据列:筛选单个数据列、筛选多个数据列、列表表达式方法、矩阵式下标方法
基于ModelArts的StyleGAN3生成高清图丨【华为云至简致远】
腾讯,投了个 “离诺贝尔奖最近的华人”
【小码匠自习室】CSP-J/S复试高分秘诀经验分享
京东三面惨遭被虐,关于redis,高并发,分布式,问懵了
机器学习+深度学习笔记(持续更新~)
KMP Media Group South Africa implemented a DMS (Document Management System) to digitize the process, employees can again focus on their actual tasks, providing efficiency
itk中生成drr整理
QWebAssembly中文适配
【os.path】的相关用法(持更)
2022-08-07 第五小组 顾祥全 学习笔记 day31-集合-Map集合
【小码匠自习室】ABC179-C:代码竟然没排倒数堪称一大奇迹
客户案例 | 提高银行信用卡客户贡献率