Set<K> keySet() //返回值是个只存放key值的Set集合(集合中无序存放的)

Set<Map.Entry<K,V>> entrySet() //返回映射所包含的映射关系的Set集合(一个关系就是一个键-值对),就是把(key-value)作为一个整体一对一对地存放到Set集合当中的。

1.keySet()

     
Map map=new HashMap();

     
Iterator it=map.keySet().iterator();

     
Object key;

     
Object value;

     
while(it.hasNext()){

     
key=it.next();

    
value=map.get(key);

     
System.out.println(key+":"+value);

  
}

 


2.entrySet()

 

    
Map map=new HashMap();

   
Iterator it=map.entrySet().iterator();

   
Object key;

   
Object value;

   
while(it.hasNext()){

   
Map.Entry entry = (Map.Entry)it.next();

   
key=entry.getKey();

   
value=entry.getValue();

   
System.out.println(key+"="+value);

 
}

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);
}
}

keySet,entrySet用法 以及遍历map的用法的更多相关文章

  1. entrySet用法 以及遍历map的用法

    entrySet用法 以及遍历map的用法   keySet是键的集合,Set里面的类型即key的类型entrySet是 键-值 对的集合,Set里面的类型是Map.Entry   1.keySet( ...

  2. TestNG entryset的用法及遍历map的用法

    以下内容引自 http://blog.csdn.net/bestone0213/article/details/47904107  (注: 该 url不是原出处.其博主注明转载,但未注明转自何处) k ...

  3. [集合]Map的 entrySet() 详解以及用法(四种遍历map的方式)

    Entry 由于Map中存放的元素均为键值对,故每一个键值对必然存在一个映射关系. Map中采用Entry内部类来表示一个映射项,映射项包含Key和Value (我们总说键值对键值对, 每一个键值对也 ...

  4. Map的两张遍历方法 keySet(),entrySet()

    源博客 http://blog.csdn.net/liu826710/article/details/9001254 在Map集合中 values():方法是获取集合中的所有的值----没有键,没有对 ...

  5. Map.Entry用法示例

    一般在HashMap中可以通过key值得到value值,以key作为检索项.Map.Entry<K,V>可以作为条目的检索项.HashMap中有entrySet()方法,返回值是Set&l ...

  6. 另一种遍历Map的方式: Map.Entry 和 Map.entrySet()

    源网址: http://blog.csdn.net/mageshuai/article/details/3523116 今天看Think in java 的GUI这一章的时候,里面的TextArea这 ...

  7. c++map的用法 分类: POJ 2015-06-19 18:36 11人阅读 评论(0) 收藏

    c++map的用法 分类: 资料 2012-11-14 21:26 10573人阅读 评论(0) 收藏 举报 最全的c++map的用法 此文是复制来的0.0 1. map最基本的构造函数: map&l ...

  8. Map基本用法

    Map的基本用法 map内部使用的是红黑树,在map内部所有的数据都是有序的 map插入有三种方法: insert(pair<int,string>(i,str)); myMap.inse ...

  9. STL中map的用法

    map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候 ...

随机推荐

  1. c++学习笔记(七)- lambda表达式 迭代器 算法

    关于lambda表达式: 刷题的时候遇到一句代码不懂: char ch = *it;auto it2 = find_if(it, b.end(), [ch](char x){ return x != ...

  2. 关于SqlCommand对象的2个方法:ExecuteNonQuery 方法和ExecuteScalar方法

    1.SqlCommand.ExecuteNonQuery 方法 对连接执行 Transact-SQL 语句并返回受影响的行数. 语法:public override int ExecuteNonQue ...

  3. 《nodejs开发指南》微博实例express4.x版

    之前一直执着于前端开发,最近几天,开始学起了nodejs.作为一名前端开发者,见到这样一门用javascript写的后台自然是很激动的.但是,后台毕竟不同于前端,在学习的过程中,还是会遇到不少问题. ...

  4. python 类似java的三目运算符

    python中没有其他语言中的三元表达式,不过有类似的实现方法 其他语言中,例如java的三元表达式是这样 int a = 1; String b = ""; b = a > ...

  5. Impala 学习

    Impala 基础知识介绍与学习,参考文章: Impala-大数据时代快速SQL引擎 https://blog.csdn.net/kangkangwanwan/article/details/7865 ...

  6. Fabric架构:抽象的逻辑架构与实际的运行时架构

    Fabric从1.X开始,在扩展性及安全性上面有了大大的提升,且新增了诸多的新特性: 多通道:支持多通道,提高隔离安全性. 可拔插的组件:支持共识组件.权限管理组件等可拔插功能. 账本数据可被存储为多 ...

  7. flask框架----设置配置文件的几种方式

    设置配置文件的几种方式 ==========方式一:============ app.config['SESSION_COOKIE_NAME'] = 'session_lvning' #这种方式要把所 ...

  8. 发布webservice服务

    1,定义一个接口 public interface HaiService { //定义一个方法 String speak(String str); } 2,编写一个实现类 import javax.j ...

  9. Spring 注入的两种方式

    Spring 的两种注入方式: 1. 属性注入:通过无参构造函数+setter方法注入 2. 构造注入:通过有参的构造函数注入. 优缺点: 1. 属性注入直白易懂,缺点是对于属性可选的时候,很多个构造 ...

  10. Myeclipse错误:Errors occurred during the build. Errors running builder 'DeploymentBuilder' on project 'MyCastBoxAPP' java.lang.NullPointerException

    报错起因: 当需要替某项目更名(即右键选定项目后选择Refactor->Rename),点击OK后,发生了中断,提示Undo抑或Abort,无论选择哪个,之后都将弹出以下错误提示框 错误描述: ...