Java 迭代映射
Iterate Map 正在从 Map 集合中提取一堆数据。在大多数实时情况下,我们在地图上存储了大量数据。开发者想要获取这些数据;他必须迭代整个地图。地图存储在 util.Map 包中。在本主题中,我们将学习 Java Iterate Map。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
您可以通过 3 种方式迭代地图。
- 对于每个
- 普通 for 循环
- 使用迭代器循环时
什么是地图?
1. Map是一个键值对的结构化集合。地图是一个界面。因此,Map 实现为
- 哈希映射
- LinkedHashMap
- 树形图
2. Entry是Map的子接口。因此,可以通过条目名称来访问它。它返回了地图的集合视图。地图。该条目提供了获取键值对的方法。
重要方法
- put(Object keyValue, Object objectValue): 它将一些值插入到映射中。
- keySet(): 用于获取密钥对集。
- entrySet(): 用于设置地图的键和值。
- size(): 用于获取地图大小。
- getKey():用于获取键值。
- getValue():用于获取键映射值。
Map 中的迭代是如何工作的?
如上所述,Map 可以使用 forEach、普通 for 和 while 循环进行迭代。我们通过语法来理解它。
1.对于每个
语法:
Map<DataType, DataType>map = new HashMap<DataType, DataType>(); map.forEach((key, value) -> //logic
2.对于 with EntrySet()
语法:
Map<DataType,DataType> map= new HashMap<DataType,DataType>(); for (Map.Entry<DataType,DataType> set: map.entrySet()) //logic }
3.带迭代器的 While 循环
语法:
Map<String, String> mapObj = new HashMap<>(); Iterator iterator= mapObj .entrySet().iterator(); while (iterator.hasNext()) { //logic }
根据需求,我们可以通过以上任意一种方式迭代地图。
Java 迭代映射示例
以下是示例:
1.使用 forEach 进行映射迭代示例
代码:
import java.util.HashMap; import java.util.Map; public class ForEachMapIteration { public static void main(String[] args) { // creating a hashmap object Map<Integer, String> names = new HashMap<>(); // adding key and values to hash map names.put(1, "Paramesh"); names.put(2, "Amardeep"); names.put(3, "Venkatesh"); names.put(4, "Ramesh"); names.put(5, "Suresh"); names.put(3, "Krishna"); names.put(4, "Rama"); names.put(5, "Rajitha"); // iterating key and value with forEach loop names.forEach((key, value) -> { System.out.println("ID =>" + key + " Name => " + value); }); } }
输出:
说明:
正如您在上面的代码中看到的,我们使用 forEach 迭代了地图
2.使用 forEach 循环分别映射键和值迭代示例
代码:
package com.map; import java.util.HashMap; import java.util.Map; public class ForEachKeyValueMapIteration { public static void main(String[] args) { // creating a hashmap object Map<Integer, String> names = new HashMap<>(); // adding key and values to hash map names.put(1, "Paramesh"); names.put(2, "Amardeep"); names.put(3, "Venkatesh"); names.put(4, "Ramesh"); names.put(5, "Suresh"); names.put(3, "Krishna"); names.put(4, "Rama"); names.put(5, "Rajitha"); // iterating ids with forEach loop names.forEach((key, value) -> { System.out.println("ID =>" + key); }); // iterating names with forEach loop names.forEach((key, value) -> { System.out.println("Name => " + value); }); } }
输出:
说明:
正如您在上面的输出中看到的,我们还可以使用 forEach 循环分别获取键和值。
3.使用 for 循环进行映射迭代示例
代码:
<strong> </strong>import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class ForMapIteration { public static void main(String[] args) { // creating a hashmap object Map<Integer, String> names = new HashMap<>(); // adding key and values to hash map names.put(1, "Paramesh"); names.put(2, "Amardeep"); names.put(3, "Venkatesh"); names.put(4, "Ramesh"); names.put(5, "Suresh"); names.put(3, "Krishna"); names.put(4, "Rama"); names.put(5, "Rajitha"); // iterating key and value with for loop for (Entry<Integer, String> entry : names.entrySet()) System.out.println("ID => " + entry.getKey() + " Name => " + entry.getValue()); } }
输出:
说明:
正如您在上面的代码中看到的,我们使用 for 循环迭代了地图。
4.使用 for 循环分别映射键和值迭代示例
代码:
<strong> </strong>import java.util.HashMap; import java.util.Map; public class ForKeyAndValueMapIteration { public static void main(String[] args) { // creating a hashmap object Map<Integer, String> names = new HashMap<>(); // adding key and values to hash map names.put(1, "Paramesh"); names.put(2, "Amardeep"); names.put(3, "Venkatesh"); names.put(4, "Ramesh"); names.put(5, "Suresh"); names.put(3, "Krishna"); names.put(4, "Rama"); names.put(5, "Rajitha"); //fetching ids for (Integer id : names.keySet()) System.out.println("ID => " + id); // fetching names for (String name : names.values()) System.out.println("Name => " + name); } }
输出:
说明:
正如您在上面的输出中看到的,我们还可以使用 for 循环分别获取键和值。
5.使用 while 循环和迭代器进行映射迭代示例
代码:
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class WhileIteratorLoopMap { public static void main(String[] args) { // creating a hashmap object Map<Integer, String> names = new HashMap<>(); // adding key and values to hash map names.put(1, "Paramesh"); names.put(2, "Amardeep"); names.put(3, "Venkatesh"); names.put(4, "Ramesh"); names.put(5, "Suresh"); names.put(3, "Krishna"); names.put(4, "Rama"); names.put(5, "Rajitha"); //get entry set from map Set set = names.entrySet(); //get iterator from set Iterator iterator = set.iterator(); //fetching id and names with while loop while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); System.out.println("ID =>"+entry.getKey() + " Name => " + entry.getValue()); } } }
输出:
说明:
正如您在上面的代码中看到的,我们使用迭代器和 while 循环迭代了映射。
注意:所有情况都会产生相同的输出,但建议使用 forEach 循环进行开发,因为它的运行时间较短。结论 – Java 迭代映射
您可以使用 Entry 接口中的 for 循环、forEach 循环和 while 循环来迭代地图。我们还可以分别迭代键和值,不会出现任何错误。
以上是Java 迭代映射的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undress AI Tool
免费脱衣服图片

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)
![您目前尚未使用附上的显示器[固定]](https://img.php.cn/upload/article/001/431/639/175553352135306.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
Ifyousee"YouarenotusingadisplayattachedtoanNVIDIAGPU,"ensureyourmonitorisconnectedtotheNVIDIAGPUport,configuredisplaysettingsinNVIDIAControlPanel,updatedriversusingDDUandcleaninstall,andsettheprimaryGPUtodiscreteinBIOS/UEFI.Restartaftereach

XSLT参数是通过外部传递值来实现动态转换的关键机制,1.使用声明参数并可设置默认值;2.从应用程序代码(如C#)通过XsltArgumentList等接口传入实际值;3.在模板中通过$paramName引用参数控制条件处理、本地化、数据过滤或输出格式;4.最佳实践包括使用有意义的名称、提供默认值、分组相关参数并进行值验证。合理使用参数可使XSLT样式表具备高复用性和可维护性,相同样式表能根据不同输入产生多样化输出结果。

AdeadlockinJavaoccurswhentwoormorethreadsareblockedforever,eachwaitingforaresourceheldbytheother,typicallyduetocircularwaitcausedbyinconsistentlockordering;thiscanbepreventedbybreakingoneofthefournecessaryconditions—mutualexclusion,holdandwait,nopree
![未找到操作系统[固定]](https://img.php.cn/upload/article/001/431/639/175539300224489.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
ifyourcomputershows“ operatatingsystemnotfound”,turtheSesteps:1.CheckBios/uefibootorder.2.verifydiskConnections.3.RepairbootLoaderSiversingWindowsRecovery.4.reassignDriveletterterterterterviadiskmanagement.5.ReinStallTheStalTheStaltheStallTheStallTheStallatingSystemyStemyfecteyStemifnecterifnecterifnequenecters。

Java设计模式是解决常见软件设计问题的可复用方案。1.Singleton模式确保一个类只有一个实例,适用于数据库连接池或配置管理;2.Factory模式解耦对象创建,通过工厂类统一生成对象如支付方式;3.Observer模式实现自动通知依赖对象,适合事件驱动系统如天气更新;4.Strategy模式动态切换算法如排序策略,提升代码灵活性。这些模式提高代码可维护性与扩展性但应避免过度使用。

TheOilPaintfilterinPhotoshopisgreyedoutusuallybecauseofincompatibledocumentmodeorlayertype;ensureyou'reusingPhotoshopCS6orlaterinthefulldesktopversion,confirmtheimageisin8-bitperchannelandRGBcolormodebycheckingImage>Mode,andmakesureapixel-basedlay

Micronautisidealforbuildingcloud-nativeJavaapplicationsduetoitslowmemoryfootprint,faststartuptimes,andcompile-timedependencyinjection,makingitsuperiortotraditionalframeworkslikeSpringBootformicroservices,containers,andserverlessenvironments.1.Microna
