map是Java中非常常用的一种数据结构,但map不同于set和list都继承自Collection接口。

所以map没有实现Collection的Iterator 方法,自身没有迭代器来遍历元素。

构造一个map

  1. Map<String, String> map = new HashMap<String, String>();
  2. map.put("001", "hello");
  3. map.put("002", "world");
  4. map.put("003", "main");

想遍历这个map,那map通常的遍历方式有哪几种呢?

这里我们介绍常用的四种方式:keySet集合迭代,entrySet集合迭代,keySet 集合for-each 循环,entrySet集合for-each循环。

方法1 keySet集合迭代

  1. // method1
  2. Set<String> keySet = map.keySet();
  3. Iterator<String> it = keySet.iterator();
  4. while (it.hasNext()) {
  5. String key = it.next();
  6. System.out.println(key + "=" + map.get(key));
  7. }

方法2 entrySet集合迭代

  1. // method2
  2. Set<Map.Entry<String, String>> entrySet = map.entrySet();
  3. Iterator<Map.Entry<String, String>> meIt = entrySet.iterator();
  4. while (meIt.hasNext()) {
  5. Entry<String, String> entry = meIt.next();
  6. System.out.println(entry.getKey() + "=" + entry.getValue());
  7. }

方法3 keySet集合for-each循环

  1. // method3
  2. for (String key : map.keySet()) {
  3. System.out.println(key + "=" + map.get(key));
  4. }

方法4 entrySet集合for-each循环

  1. // method4
  2. for (Map.Entry<String, String> entry : map.entrySet()) {
  3. System.out.println(entry.getKey() + "=" + entry.getValue());
  4. }

四种方式中,method1 和 method2 是通过迭代器来显示完成的,method3 和 method4 是通过for-each来隐式的通过迭代器来完成的。

同时 method1 和 method3 是通过key的集合来完成的,method2 和 method4 是通过entry 的集合来完成的。

方法1 和方法2 的区别

一个是获取keySet ,一个是获取entrySet

推荐使用entrySet 的方式去获取,查看map通过key获取value的方法

  1. public V get(Object key) {
  2. if (key == null)
  3. return getForNullKey();
  4. Entry<K,V> entry = getEntry(key);
  5. return null == entry ? null : entry.getValue();
  6. }

也是先获取该key对应的entry,然后再获取value值,所以,推荐使用entrySet 方法,再遍历entry集合的方式来遍历map

map 接口还有一个方法 values() ,由于仅能取到所有的value值,而取不到key值,所以在这里就算不上遍历map了,只能算上遍历map的value值。

map的四种遍历方式的更多相关文章

  1. Map 的四种遍历方式

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

  2. java Map的四种遍历方式

    1.这是最常见的并且在大多数情况下也是最可取的遍历方式,在键值都需要时使用. Map<Integer, Integer> map = new HashMap<Integer, Int ...

  3. list的四种遍历方式

    1.手先增强for循环和iterator遍历的效果是一样的,也就说 增强for循环的内部也就是调用iteratoer实现的,但是增强for循环 有些缺点,例如不能在增强循环里动态的删除集合内容.不能获 ...

  4. Java中Map的4种遍历方式

    第一种方式:这是平常用的最多也最可取的一种遍历方式. for (Map.Entry<String, Object> entry : map.entrySet()) { System.out ...

  5. lua中for循环的四种遍历方式

    lua中for的四种遍历方式区别 table.maxn 取最大的整数key #table 从1开始的顺序整数最大值,如1,2,3,6 #table == 3   key,value pairs 取每一 ...

  6. Map的四种遍历

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

  7. java集合四种遍历方式

    package conection; import java.util.Iterator;import java.util.LinkedList;import java.util.List; publ ...

  8. java map的四种遍历

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

  9. Map的两种遍历方式

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

随机推荐

  1. javascript中的窗口和框架

    框架: 在网络上我们可以看到很多WEB应用程序都是使用框架(frame)来分隔浏览器窗口的,就想一块块玻璃隔板把窗口分隔成好几个小窗口,并且可以在不同的小窗口中加载显示不同的页面,这样在我们看来好像是 ...

  2. ThinkPHP开启事物

    $m=D('YourModel');//或者是M(); $m2=D('YouModel2'); $m->startTrans();//在第一个模型里启用就可以了,或者第二个也行 $result= ...

  3. 参考__MySql

    博客 三范式 事务隔离级别 列表

  4. js中call、apply、bind的用法

    原文链接:http://www.cnblogs.com/xljzlw/p/3775162.html var zlw = { name: "zlw", sayHello: funct ...

  5. css3 文字过长用...代替

    white-space: nowrap; text-overflow: ellipsis; overflow: hidden; white-space:nowrap 代表多个空格/回车不换行: tex ...

  6. Eenterprise linux服务器分区

    分区说明: (在MBR格式的硬盘下我会分/ /boot swap /data 四个分区,不建议在服务器上面使用LVM,中大型企业的IDC都是有存储区域的,专门管理硬盘容量的.)(分区的时候,请注意顺序 ...

  7. ROS学习笔记(四)——环境变量配置

    1.查看环境变量配置情况,其实并没有什么卵用 $ export | grep ROS 或者用 $ printenv | grep ROS2.配置环境变量??$ source /opt/ros/indi ...

  8. centos6.7 mutlipath install script

    #!/bin/bash if [ `id -g` != 0 ] ;then echo -e "\033[31m Please use root user\033[0m" exit ...

  9. grep 使用或条件 ( grep -e )

    test@k1rhel5822161:/home/test>cat 31 52 33 24567test@k1rhel5822161:/home/test>grep -e '2|3' 3t ...

  10. jquery阻止元素冒泡的两种方法

    通常情况下,如果给父元素添加事件之后,子元素也会继承同样的事件,这个时候就要阻止子元素的这种行为,成为阻止冒泡,总结两种解决方法: html代码: <div id="parent&qu ...