当前位置:网站首页>The four traversal methods of the map set can be seen at a glance
The four traversal methods of the map set can be seen at a glance
2022-06-29 03:25:00 【Su bro】
Traverse Map And traversal List There's a big difference , Beginners have never touched map You may not understand the code , I'd like to make a brief statement here .
Map The element stored in is in the form of “ Key value pair ” The way , That is to say key-value Yes .
Map.Entry yes Map One of the interfaces in , Its purpose is to represent a mapping item ( There are Key and Value), As shown in the figure below :
So we can traverse through Map Of entrySet() Method , It returns an implementation Map.Entry Object collection of interface , Every entry Object to store the pair K-V Yes . We can call... Through this object getKey() and getValue() Get what you need easily key and value
The second and third traversal methods in the following code are used entrySet() Method
//Map Four ways to traverse a set
public class MapDemo {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("tom",11);
map.put("cindy",22);
map.put("marry",42);
//1、 The first traversal method , obtain key--> to value
for(String key : map.keySet()){
//keySet() Returns the... Of the key contained in this map Set View
System.out.print(key+"="+ map.get(key)+" ");
}
//2、 The second traversal method , adopt Map.entrySet Use iterator Traverse key and value
Set<Map.Entry<String, Integer>> entries = map.entrySet(); //entrySet() Returns the... Contained in this map entry Of Set View
Iterator<Map.Entry<String, Integer>> iterator = entries.iterator();
while (iterator.hasNext()){
Map.Entry<String, Integer> next = iterator.next();
System.out.print(next.getKey()+"="+next.getValue()+" ");
}
//3、 The third way of traversal , adopt Map.entrySet Traverse key and value
for (Map.Entry<String,Integer> entry : map.entrySet()){
System.out.print(entry.getKey()+"="+entry.getValue()+" ");
}
//4、 The fourth way of traversal , adopt Map.values() Traverse all value, But you can't traverse key
for (Integer integer: map.values()){
System.out.print("value="+integer+" ");
}
}
}
边栏推荐
- 图论的基本概念
- 不同的二叉搜索樹[自下而上回溯生成樹+記憶搜索--空間換時間]
- gcc编译器包
- Is it safe for qiniu school to open an account in 2022?
- 99 multiplication table
- Get error: Unsupported fork ordering: eip150block not enabled, but eip155block enabled at 0
- Différents arbres de recherche binaires [arbre de génération rétrospectif ascendant + recherche de mémoire - - espace - temps]
- Linear and nonlinear structures
- VIM configuration and use
- 初探元宇宙存储,数据存储市场下一个爆点?
猜你喜欢

问题——adb shellerror: insufficient permissions for device: verify udev rules.
![[test theory] quality analysis ability](/img/4b/d011e16c7b2be52fe12c123214779e.jpg)
[test theory] quality analysis ability

Web APIs 高阶函数 丨黑马程序员

vim配置与使用

2022-2028 global long wave infrared camera and camera core industry research and trend analysis report

MySQL advanced SQL statement (Part 2)

Tu ne peux pas comprendre le feu?

Deeply analyzing the business logic of "chain 2+1" mode

Linear and nonlinear structures

2D人体姿态估计 - DeepPose
随机推荐
LeetCode 每日一题——324. 摆动排序 II
2022-2028 global industrial lithium chloride industry research and trend analysis report
In depth analysis of Apache bookkeeper series: Part 3 - reading principle
19.03 容器的说明和简单应用例续
Tupu software intelligent energy integrated management and control platform
Etcd tutorial - Chapter 6 etcd core API V3
Jerry's monitoring alarm clock [chapter]
SVN常用的十个命令
快速排序,查询序列的第K大的数
搭建nexus服务
Démarrer le test - test d'intégration
How to add the live video function to the website built by your own live video software (build a live video website)
FPGA (VIII) RTL code IV (basic circuit design 1)
[Shangshui Shuo series] the simplest subtitle configuration
Gartner's "voice of customers" has the highest score, and the user experience has become a major breakthrough for China's database
測試入門——集成測試
Linear and nonlinear structures
Supplement to the scheme of gateway+nacos+knife4j (swagger)
嵌入式开发如何进行源代码保密工作
Etcd教程 — 第七章 Etcd之事务API