遍历Map的四种方法

news/2024/7/4 12:14:13

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

public static void main(String[] args) {


  Map<String, String> map = new HashMap<String, String>();
  map.put("1", "value1");
  map.put("2", "value2");
  map.put("3", "value3");
  
  //第一种:普遍使用,二次取值
  System.out.println("通过Map.keySet遍历key和value:");
  for (String key : map.keySet()) {
   System.out.println("key= "+ key + " and value= " + map.get(key));
  }
  
  //第二种
  System.out.println("通过Map.entrySet使用iterator遍历key和value:");
  Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
  while (it.hasNext()) {
   Map.Entry<String, String> entry = it.next();
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }
  
  //第三种:推荐,尤其是容量大时
  System.out.println("通过Map.entrySet遍历key和value");
  for (Map.Entry<String, String> entry : map.entrySet()) {
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }

  //第四种
  System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
  for (String v : map.values()) {
   System.out.println("value= " + v);
  }
 }


转载于:https://my.oschina.net/u/1177694/blog/537899


http://lihuaxi.xjx100.cn/news/244078.html

相关文章

WPS 2019 更新版(8392)发布,搭配优麒麟 19.04 运行更奇妙!

WPS 2019 支持全新的外观界面、目录更新、方框打勾、智能填充、内置浏览器、窗口拆组、个人中心等功能。特别是全新的新建页面&#xff0c;让你可以整合最近打开的文档、本地模版、公文模版、在线模板等。 随着优麒麟即将发布的新版 19.04 的到来&#xff0c;金山办公软件也带来…

java对图片按照指定尺寸压缩

1.pom.xml引入依赖 <dependency><groupId>net.coobird</groupId><artifactId>thumbnailator</artifactId><version>0.4.8</version></dependency> 2.接口 /*** 上传人员头像* param multipartFile* return*/Log(title &qu…

Linux man命令

【功能说明】&#xff1a;详细记录着命令或文件的操作说明&#xff08;manual&#xff09;【语法格式】&#xff1a;man [命令|文件名]【实践操作】&#xff1a;man ls常用代号说明代号 代表内容1User Commands&#xff08;一般用户可使用的命令&#xff1a;ls&#xff09;4Lin…

runtime实践之Method Swizzling

利用 Objective-C 的 Runtime 特性&#xff0c;我们可以给语言做扩展&#xff0c;帮助解决项目开发中的一些设计和技术问题。这一篇&#xff0c;我们来探索一些利用 Objective-C Runtime 的黑色技巧。这些技巧中最具争议的或许就是 Method Swizzling 。 介绍一个技巧&#xff0…

适用于Mac上的SQL Server

适用于Mac上的SQL Server&#xff1f; 众所周知&#xff0c;很多人都在电脑上安装了SQL Server软件&#xff0c;普通用户直接去官网下载安装即可&#xff0c;Mac用户则该如何在Mac电脑上安装SQL Server呢&#xff1f;想要一款适用于Mac上的SQL Server&#xff1f;点击进入&…

SpringBoot+Vue实现学生管理系统

1.技术架构 前后端分离项目&#xff0c;后台&#xff1a;springBootmysqlmybatis 前端&#xff1a;vue框架 2.安装环境 IDEA/eclipse均可 jdk1.7或1.8 maven项目 mysql数据库版本为8.0.17 node 3.功能说明 用户角色&#xff1a;学生、家长或教师、管理员 学生&#…

Python开发环境配置

好久没有写博客了&#xff0c;自从6月份毕业后&#xff0c;进入一家做书法、字画文化宣传的互联网公司&#xff08;www.manyiaby.com&#xff09;&#xff0c;这段时间一直在进行前端开发&#xff0c;对于后端的使用很少了&#xff0c;整天都是什么html、css、javascript、jque…

hdu 1247

Problem DescriptionA hat’s word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary.You are to find all the hat’s words in a dictionary.InputStandard input consists of a number of lowercase words, one per li…