Map集合遍历

  Map<String,Integer> m = new HashMap<String,Integer>();

    m.put("one",100);

    m.put("two",200);

    m.put("three",300);

法一:

  法一涉及到的方法keySet()

    for(String s : m.keySet){               //keySet()获取key的集合;

    System.out.print("keys: "+s);

    System.out.print("values: "+m3.get(s)); //并通过get(Object key)获取对应的value的值。

  }

法二:

  法二涉及到的方法values()

  for(Integer t : m.values()){

    System.out.print("values: "+t);   //values()获取value的集合

  }

法三:

  法三涉及到Map接口的内部接口Entry接口,涉及到Entry接口的getKey()方法和getValue()方法

  for(Map.Entry<String,Integer> entry : m.entrySet()){

    System.out.print("keys: "+entry.getKey()+" values: "+entry.getValue());

  }

法四:

  使用迭代器,涉及到Map接口的内部接口Entry接口,涉及到Entry接口的getKey()方法和getValue()方法

  Set set = m.entrySet();

  Iterator it = set.iterator();

  while(it.hasNext()){

    Map.Entry<String,Integer> entry = (Map.Entry<String,Integer> entry)it.next();

    System.out.print("keys: "+entry.getKey()+" values: "+entry.getValue());

  }

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. Map集合的两种遍历方式

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

  4. Map集合的几种遍历方式

    Map<String ,String> map=new HashMap<String,String>(); map.put("1","value1 ...

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

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

  6. Map集合的四种常用遍历方式整理

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

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

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

  8. Map集合的4种遍历方式

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

  9. Map<String,String>集合的四种遍历方式 其中有一种针对大容量的数据集合

随机推荐

  1. ubuntu12.04已安装SQLite3 而简单易用

    今天想写一点app,使用数据库,所以在这里简要地记住它是安装和使用. 1.安装SQLite3 命令行下输入:sudo apt-get install sqlite3 2.安装SQLite3编译须要的工 ...

  2. Bluetooth in Android 4.2 and 4.3(三):Enable Bluetooth

    以下是基于Android 4.2代码,对Bluetooth BR/EDR Enable process的分析.BluetoothAdapter类代表的是local device Bluetooth a ...

  3. spinner中的onNothingSelected方法到底什么时候调用?

    这个东东大家在开发中可能不太能用到,所以总是容易被忽略,由于工作原因,我最近琢磨了一下onNothingSelected方法的调用时机问题,其实很简单,只要我们稍微看一下源码就明白了: /** * C ...

  4. 使用DrawerLayout实现QQ5.0侧拉菜单效果

    在上一篇文章中,我们介绍了怎么使用DrawerLayout来实现一个简单的侧拉菜单(使用DrawerLayout实现侧拉菜单),也就是我们常说的抽屉效果,GitHub上类似效果的实现方式非常多,实现出 ...

  5. Socket.io各个发送消息的含义

    // send to current request socket client socket.emit('message', "this is a test"); // send ...

  6. json 转对象

    架包: import com.alibaba.fastjson.JSON; String arryStr="[{\"Name\": \"A\", \& ...

  7. html Js跨域提交数据方法,跨域提交数据后台获取不到数据

    MVC实现方式:(后台获取不到方法请参考下面js) [ActionAllowOrigin][HttpPost]public JsonResult Cooperation() { return json ...

  8. ios Object Encoding and Decoding with NSSecureCoding Protocol

    Object Encoding and Decoding with NSSecureCoding Protocol February 27, 2014 MISC NSCoding is a fanta ...

  9. JavaScript高级程序设计(五): js的关键字instanceof和typeof使用

    JavaScript中instanceof和typeof 常用来判断一个变量是否为空,或者是什么类型的.但它们之间还是有区别的: 一.typeof 1.含义:typeof返回一个表达式的数据类型的字符 ...

  10. [python] 字符串与列表、字典的转换

    1.字符串->字典:eval(str) 2.字符串->列表:list(str)