Java遍历Map的3种方式
package test; import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import org.junit.Before;
import org.junit.Test; /**
* 对于Map的三种方式遍历 1.keySet() 2.values() 3.entrySet()
* 三种方式得到Set之后,都可以使用 foreach或者iterator, 不能使用for,因为数据结构决定的
*
* @author Administrator
*
*/
public class MapCycle { Map<Integer, String> map; // 准备好数据
@Before
public void testData() {
map = new HashMap<Integer, String>();
map.put(1, "张学友");
map.put(2, "刘德华");
map.put(3, "郭富城");
map.put(4, "黎明");
map.put(5, "周润发");
} /** 测试三种方式,这三种方式最后都是遍历Set,于是都可以使用 foreach或者Iterator **/
// 方式1: keySet()方法获取到Set(key)
@Test
public void testFirst() {
Set<Integer> set = map.keySet();
for (Integer integer : set) {
System. out.println( map.get(integer));
}
} // 方式2:values()方法获取到Collection(value)
@Test
public void testSecond() {
Collection<String> collection = map.values();
for (String string : collection) {
System. out.println(string);
}
} // 方式3:entrySet()方法获取到Set<Entry<key,value>>
@Test
public void testThird() {
Set<Entry<Integer, String>> entries = map.entrySet();
for (Entry<Integer, String> entry : entries) {
System. out.println(entry.getValue());
}
}
}
Java遍历Map的3种方式的更多相关文章
- java 遍历Map的四种方式
java 遍历Map的四种方式 CreationTime--2018年7月16日16点15分 Author:Marydon 一.迭代key&value 第一种方式:迭代entrySet 1 ...
- java遍历Map的几种方式
1.遍历map的几种方式:private Hashtable<String, String> emails = new Hashtable<String, String>(); ...
- java遍历map的四种方式
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
- Java遍历Map的4种方式
public static void main(String[] args) { // 循环遍历Map的4中方法 Map<Integer, Integer> map = new HashM ...
- [转载] Java 遍历 Map 的 5 种方式
目录 1 通过 keySet() 或 values() 方法遍历 2 通过 keySet 的 get(key) 获取值 3 通过 entrySet 遍历 4 通过迭代器 Iterator 遍历 5 通 ...
- Java遍历map的五种方式
使用For-Each迭代entries 这是最常见的方法,并在大多数情况下更可取的.当你在循环中需要使用Map的键和值时,就可以使用这个方法 Map<Integer, Integer> m ...
- [JAVA]JAVA遍历Map的几种方式
//遍历key for (String key : dic.keySet() ) { System.out.println(key + dic.get(key)); } //遍历values for ...
- 遍历map的几种方式
1,平时开发中对map的使用很多,然后发现了很多map可能存在的各种问题:如HashMap 需要放置 1024 个元素,由于没有设置容量初始大小,随着元素不断增加,容量 7 次被迫扩大,resize ...
- 遍历map的6种方式
1,平时开发中对map的使用很多,然后发现了很多map可能存在的各种问题:如HashMap 需要放置 1024 个元素,由于没有设置容量初始大小,随着元素不断增加,容量 7 次被迫扩大,resize ...
随机推荐
- 微信小店分类ID列表
一级分类 { , "errmsg": "ok", "cate_list": [ { ", "name": &q ...
- jenkins邮件模板
步骤 1.在jenkins主目录中新建一个模板文件夹 命名为:email-templates 3.把模板代码放入到模板文件夹 with_results.groovy 4.设置邮件发送模板配置 5.配 ...
- Python线程指南
本文介绍了Python对于线程的支持,包括“学会”多线程编程需要掌握的基础以及Python两个线程标准库的完整介绍及使用示例. 注意:本文基于Python2.4完成,:如果看到不明白的词汇请记得百度谷 ...
- 百度地图API 简单示例
百度地图API2.0需要申请AK javascript引用百度地图API 设置地图DIV样式 javascript设置地图DIV 如下图代码: <!DOCTYPE html> <ht ...
- [算法]Comparison of the different algorithms for Polygon Boolean operations
Comparison of the different algorithms for Polygon Boolean operations. Michael Leonov 1998 http://w ...
- Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- kfed (kernel file editor:内核文件编辑器)
kfed是没有在文档中标出的asm工具,在oracle 11gR1中被引入.可以被用来读写asm元数据,特别是磁盘头和asm元数据的内容. kfed是一个单独的工具,不依赖与asm实例,所以可以对mo ...
- Oracle Flashback Technologies (总)
Oracle Flashback Technologies Oracle 9i中增加了闪回查询技术,闪回查询为数据库提供了一种简单.强大.完全无干扰从人为错误中恢复的机制.通过闪回查询,用户可以查看过 ...
- 利用DescriptionAttribute实现枚举字符串
我们知道定义枚举时是不允许带空格等符号的,这样就不利于进行字符串对比.当然可以通过给枚举添加DescriptionAttribute,然后通过fieldinfo读取DescriptionAttribu ...
- Lintcode: Update Bits
Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits be ...