Map,HashMap五种遍历方法
假设有数组
HashMap<Integer, String> h=new HashMap<Integer,String>();
h.put(111, "111-");
h.put(222, "222-");
在操作之前明确几个方法的调用位置
1.keyset方法Map接口
2.get()方法,来自Map
3.Set<Map.Entry<K,V>> entrySet() 来自Map
4.Set<K> keySet() 来自Map
5.iterator() 来自 Collection
6.getValue()和getKye() 来自 Map内部类entry
//1.使用增强for 和 keySet()方法
//先使用Map接口中的keySet()方法 获取返回值set<k>对象,或者叫数组
Set<Integer> s=h.keySet();
//此时获得key值
//遍历使用增强for
for(Integer i:s)
{
//i为key
//使用Map接口中的get(key) 方法,返回值为对应的value
system.out.println(h.get(i));
}
//2.使用增强for和Iterator,keyset遍历
//先使用Map接口中的keySet()方法 获取返回值set<k>对象,或者叫数组
Set<Integer> s=h.keySet();
//使用Iterator接口中的iterator()方法,返回值为Iterator<E>对象,set继承了Collection
//所以拥有iterator()方法,可以直接使用
Iterator i=s.iterator();
//使用增强for遍历
while(i.hasnext())
{
system.out.println(s.get(i.next()));
}
//3.使用entrySet()和增强for
Set<Map.Entry<Integer, String>> s3=h.entrySet();
for(Map.Entry<Integer, String> m:s3)
{
System.out.println(m.getValue()+m.getKey());
}
//4.使用entrySet()和Iterator
Set<Map.Entry<Integer, String>> s4=h.entrySet();
Iterator<Map.Entry<Integer, String>> i2=s4.iterator();
while(i2.hasNext())
{
Map.Entry<Integer, String> a=i2.next();
System.out.println(a.getValue()+a.getKey());
}
//5.只能遍历到内容的方法
//Collection<String> c2=new ArrayList<String>();
Collection<String> c=h.values();
//ArrayList<String> a=(ArrayList<String>)c;
for(String s:c)
{
System.out.println(s);
}*/
Map,HashMap五种遍历方法的更多相关文章
- Map的五种遍历方法
package com.jackey.topic; import java.util.ArrayList;import java.util.HashMap;import java.util.Itera ...
- Java中Map的三种遍历方法
Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历. 告诉您们一个小秘密: (下↓面是测试代码,最爱看 ...
- map的三种遍历方法!
map的三种遍历方法! 集合的一个很重要的操作---遍历,学习了三种遍历方法,三种方法各有优缺点~~ /* * To change this template, choose Tools | Te ...
- Map的6种遍历方法
声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 探讨有几种遍历Map的方法其实意义并不大,网上的文章一般讲4种或5种的居多,重要的是知道遍历的内涵,从遍历 ...
- Map获取键值,Map的几种遍历方法
Map 类提供了一个称为entrySet()的方法,这个方法返回一个Map.Entry实例化后的对象集.接着,Map.Entry类提供了一个 getKey()方法和一个getValue()方法,Map ...
- Map获取键值,Map的几种遍历方法 (转载)
Map类提供了一个称为entrySet()的方法,这个方法返回一个Map.Entry实例化后的对象集.接着,Map.Entry类提供了一个getKey()方法和一个getValue()方法,Map.E ...
- hashmap两种遍历方法
第一种:使用entryset来进行遍历 Map map=new HashMap(); Iterator iter=map.entrySet().iterator(); while(iter.hasNe ...
- Map 的两种遍历方法详细说明
//方法一 Set<String> set = map.keySet(); for (String s:set) { System.out.println(s+","+ ...
- Map的四种遍历方法
1.取值遍历 for(String key:map.keySet()){ System.out.println("key="+key+"and value=" ...
随机推荐
- CentOS配置Tomcat监听80端口,虚拟主机
2019独角兽企业重金招聘Python工程师标准>>> Tomcat更改默认端口为80 更改的配置文件是: /usr/local/tomcat/conf/server.xml [ro ...
- salesforce零基础学习(九十六)Platform Event浅谈
本篇参考:https://developer.salesforce.com/blogs/2018/07/which-streaming-event-do-i-use.html https://trai ...
- 历史上的今天mysql数据库包含详情分类以及图片
历史上的今天mysql数据库包含详情分类以及图片 https://item.taobao.com/item.htm?spm=a2oq0.12575281.0.0.50111debo71iaJ& ...
- P1466 集合 Subset Sums 搜索+递推+背包三种做法
题目描述 对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合,且保证每个集合的数字和是相等的.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,每个子 ...
- POJ - 2251 Dungeon Master(搜索)
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- 2019 ICPC 南京网络赛 F Greedy Sequence
You're given a permutation aa of length nn (1 \le n \le 10^51≤n≤105). For each i \in [1,n]i∈[1,n], c ...
- 软件——Jira是什么
JIRA这个工具接触有好几年了,在多个海外项目上都用过这个工具.去年又在项目上深度使用后就有点爱不释手了,回国后也在找机会推荐给其它项目上用.最近正好有新项目需要用,借这个机会把JIRA的配置学习的过 ...
- Java 常用API(一)
目录 Java 常用API(一) 1. Scanner类 引用类型的一般使用步骤 Scanner的使用步骤 例题 2. 匿名对象 概述 匿名对象作为方法的参数 匿名对象作为方法的返回值 3. Rand ...
- 在web项目中使用shiro(认证、授权)
一.在web项目中实现认证 第一步,在web项目中导入shiro依赖的包 第二步,在web.xml中声明shiro拦截权限的过滤器 <filter> <filter-name> ...
- webpack-基础知识
一.webpack介绍 webpack是一个前端模块化工具,简单解释:webpack就是处理多个文件,根据设置的规则,对文件进行合并和修改. 正式说:webpack是一个模块化打包工具.从入口模块出发 ...