Map<String, String> map = new HashMap<String, String>();
map.put("A", "AAA");
map.put("B", "BBB");
map.put("C", "CCC");
map.put("D", "DDD");

// 第一种用for循环

for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "--->" + entry.getValue());
}

// 第二种用迭代

Set set = map.entrySet();
Iterator it = set.iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry1 = (Map.Entry<String, String>) it.next();
System.out.println(entry1.getKey() + "==" + entry1.getValue());
}

Iterator it = map.keySet().iterator();
while (it.hasNext()) {
String key;
String value;
key = it.next().toString();
value = map.get(key);
System.out.println(key + "--" + value);
}

// 用entrySet()迭代

Iterator<Entry<String, String>> it = map.entrySet().iterator();
System.out.println(map.entrySet().size());
String key;
String value;
while (it.hasNext()) {
Map.Entry entry = it.next();
key = entry.getKey().toString();
value = entry.getValue().toString();
System.out.println(key + "====" + value);
}

Map的3种遍历[轉]的更多相关文章

  1. Java中Map的三种遍历方法

    Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历.   告诉您们一个小秘密: (下↓面是测试代码,最爱看 ...

  2. Map的三种遍历

    import java.util.*;/*** Map的三种遍历方式* @author Administrator**/public class m {public static void main( ...

  3. Map的四种遍历

    //Map的四种遍历方法 public static void main(String[] args) { Map<String, String> map = new HashMap< ...

  4. Map 的四种遍历方式

    Map 的四种遍历方式 import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class ...

  5. map的三种遍历方法!

    map的三种遍历方法!   集合的一个很重要的操作---遍历,学习了三种遍历方法,三种方法各有优缺点~~ /* * To change this template, choose Tools | Te ...

  6. map的4种遍历方式

            System.out.println("key= "+ key + " and value= " + map.get(key));    }   ...

  7. java map的四种遍历

    四种遍历: public static void main(String[] args) { Map<String, String> map = new HashMap<String ...

  8. Map的三种遍历方式

    对于Map的三种方式遍历 1.keySet() 2.values() 3.entrySet()三种方式得到Set之后,都可以使用 foreach或者iterator, 不能使用for,因为数据结构决定 ...

  9. Map的两种遍历方式

    ********************************************************************************* ****************** ...

随机推荐

  1. Android 开发 之 Fragment 详解

    本文转载于 : http://blog.csdn.net/shulianghan/article/details/38064191 本博客代码地址 : -- 单一 Fragment 示例 : http ...

  2. [原创]cocos2d-x研习录—前言

    我认为很多开发者面对层出不穷的新技术.新思想和新理念,最为之苦恼的是找不到行之有效的学习方法,对于知识的本质缺乏认识,虽阅读了大量教材,却无法将其融入自己的知识体系,并搭建自己的知识树.不可否认,教材 ...

  3. 掌握 Ajax,第 1 部分: Ajax 入门简介

    转:http://www.ibm.com/developerworks/cn/xml/wa-ajaxintro1.html 掌握 Ajax,第 1 部分: Ajax 入门简介 理解 Ajax 及其工作 ...

  4. LINQ及EntityFramework何时从数据库返回数据,备忘

    Generally speaking, LINQ queries are executed when the application code processes data (for instance ...

  5. 010. 使用.net框架提供的属性

    C#允许在类和类成员上声明特性(类), 可在运行时解释类和类的成员. 这个特性也称为属性, 使用Attribute.下面演示如何使用.net框架提供的属性. using System; using S ...

  6. oracle OFA

    Optimal Flexible Architecture 完全实现OFA至少需要三个文件系统位于不同的物理设备上,这些物理设备本身没有做条带或镜像.如果这些物理设备要做冗余与吞吐,建议使用一些存储相 ...

  7. BNUOJ 1006 Primary Arithmetic

    Primary Arithmetic 来源:BNUOJ 1006http://www.bnuoj.com/v3/problem_show.php?pid=1006 当你在小学学习算数的时候,老师会教你 ...

  8. Linux下printf格式符%d、%lld、%llx、%u等【转自CSDN博客】

    来源:http://blog.csdn.net/anycell/article/details/6966520 %d 有符号32位整数 %lld 有符号64位证书 %llx有符号64位16进制整数 % ...

  9. java读写文件大全

     java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...

  10. HTTP API 设计指南(中文版) restfull

    http://www.css88.com/archives/5121 目录 基础 总是使用TLS 在Accepts头中带上版本号 通过Etags支持缓存 用Request-Ids追踪请求 用Range ...