1.Map集合简介:map集合是一个key—value型的数据结构,存储的数据具有查询速度快速的特点,但由于是无序的,所以没有顺序可言。在遍历时没有办法像简单的list或数组一样。

2.代码:

  1. package com.cn.testmap;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7.  
  8. /**
  9. * map的4种便历方法操作
  10. * @author lenovo
  11. *
  12. */
  13.  
  14. public class Maptest {
  15. private static Map<String,String> map = new HashMap<String,String>();
  16. public static void main(String[] args) {
  17. map.put("name", "李四");
  18. map.put("age", "30");
  19. map.put("sex", "male");
  20. map.put("code", "3010");
  21. //方法一:通过key取值
  22. /*for(String key:map.keySet()){
  23. System.out.printf("map key is %s and value is %s",key,map.get(key));
  24. System.out.println();
  25. }*/
  26. //方法二:通过迭代器取值
  27. /*Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
  28. Entry<String, String> entry = null;
  29. while(iterator.hasNext()){
  30. entry = iterator.next();
  31. System.out.printf("key is %s and value is %s",entry.getKey(),entry.getValue());
  32. System.out.println();
  33. }*/
  34. //通过entryset
  35. /*for(Entry<String, String> entry:map.entrySet()){
  36. System.out.printf("key is %s and value is %s",entry.getKey(),entry.getValue());
  37. System.out.println();
  38. }*/
  39. //通过map的value方法实现
  40. for(String value : map.values()){
  41. System.out.println("value is "+value);
  42. }
  43. }
  44. }

Map集合的四种常用遍历方式整理的更多相关文章

  1. Map集合的四种遍历方式

    很久以前写的代码,和上一个做比较吧!便于以后查看 import java.util.HashMap; import java.util.Iterator; import java.util.Map; ...

  2. Map集合的四种遍历方式(转载)

    import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class TestMap { pu ...

  3. Java中遍历Map集合的四种方法

    在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...

  4. Java中Map集合的四种访问方式(转)

    最近学习Java发现集合类型真是很多,访问方式也很灵活,在网上找的方法,先放下备用 public static void main(String[] args) { Map<String, St ...

  5. Map集合的四种遍历

    Map集合遍历 Map<String,Integer> m = new HashMap<String,Integer>(); m.put("one",100 ...

  6. Map集合的两种遍历方式

    Map集合:即 接口Map<K,V> map集合的两种取出方式:    1.Set<k> keyset: 将map中所有的键存入到set集合(即将所有的key值存入到set中) ...

  7. Java遍历Map对象的四种方式

    关于java中遍历map具体哪四种方式,请看下文详解吧. 方式一 :这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> m ...

  8. 【转】Java遍历Map对象的四种方式

    关于java中遍历map具体哪四种方式,请看下文详解吧. 方式一 这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> ma ...

  9. Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 [ 转载 ]

    Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 @author Trinea 原文链接:http://www.trinea.cn/android/arrayl ...

随机推荐

  1. 基本动画、复合动画设置 平移、缩放、旋转、透明度 编码实现 xml实现

    public class VAActivity extends Activity { private ImageView iv_animation; private TextView tv_anima ...

  2. iOS信号量

    引子: 在取本地联系人列表的时候看到同事用的这么一段代码: dispatch_semaphore_t sema = dispatch_semaphore_create(); ABAddressBook ...

  3. POj 3253 Fence Repair(修农场栅栏,锯木板)(小根堆 + 哈弗曼建树得最小权值思想 )

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28359   Accepted: 9213 Des ...

  4. poj3295 Tautology —— 构造法

    题目链接:http://poj.org/problem?id=3295 题意: 输入由p.q.r.s.t.K.A.N.C.E共10个字母组成的逻辑表达式, 其中p.q.r.s.t的值为1(true)或 ...

  5. Codeforces Round #363 (Div. 2) C. Vacations —— DP

    题目链接:http://codeforces.com/contest/699/problem/C 题解: 1.可知每天有三个状态:1.contest ,2.gym,3.rest. 2.所以设dp[i] ...

  6. Linux 查看服务状态(服务与进程)

    原文地址:http://blog.csdn.net/weixin_37979944/article/details/79029188 指令(instruction): 计算机实际上可以做的事情实质上非 ...

  7. mac配置apache

    http://www.cnblogs.com/snandy/archive/2012/11/13/2765381.html 用自带的 sudo apachectl -v sudo apachectl ...

  8. PHP编写网页BS结构计算器

    利用PHP和Html + javascript 编写一个简单的网页计算器程序,可以根据这种有趣的开发构架出更加有趣的网页.​1. [代码]PHP计算器    <html><head& ...

  9. 存储过程系列三:根据表别名方式distinct去重插入

    1.根据表别名方式distinct去重插入 insert into GG_XKZ_YLQXSCXKESL_SCDZ           ( bzj, xkzid,  sqid, jtdz, szsf, ...

  10. spark uniq 本质上就是单词计数

    粗体部分示例: # dns_domain_info_list_rdd ==> [(src_ip, domain, domain_ip, timestamp, metadataid), ....] ...