黑马程序员技术交流社区

标题: Map集合取数据的四种取出方法 [打印本页]

作者: 漪顿    时间: 2013-8-14 14:06
标题: Map集合取数据的四种取出方法
     // 第一种方法(传统方法)

   

        public void runOne()
     {
        Map map = new HashMap();

        map.put("01", "java01");

        map.put("02", "java02");

        map.put("03", "java03");

        Set set = map.keySet();

        Iterator it = set.iterator();

        while (it.hasNext())
       {

            String key = (String) it.next();

            String value = (String) map.get(key);

            System.out.println(key + "=" + value);

        }

    }
// 第二种方法(传统方法)

   

       public void runTwo() {

        Map map = new HashMap();

        map.put("01", "java01");

        map.put("02", "java02");

        map.put("03", "java03");

        Set set = map.entrySet();

        Iterator it = set.iterator();

        while (it.hasNext()) {

            Entry entry = (Entry) it.next();

            String key = (String) entry.getKey();

            String value = (String) entry.getValue();

            System.out.println(key + "=" + value);

        }

    }

       // 第三种方法(增强for循环方法)

   
       public void runThree() {

        Map map = new LinkedHashMap();

        map.put("01", "java01");

        map.put("02", "java02");

        map.put("03", "java03");

        for (Object obj : map.keySet()) {

            String key = (String) obj;

            String value = (String) map.get(key);

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

      // 第四种方法(增强for循环方法)
          public void runFour() {
              Map map = new LinkedHashMap();

              map.put("01", "java01");

               map.put("02", "java02");

               map.put("03", "java03");
            for (Object obj : map.entrySet()) {

            Entry entry = (Entry) obj;

            String key = (String) entry.getKey();

            String value = (String) entry.getValue();

            System.out.println(key + "=" + value);

作者: 刘劲松    时间: 2013-8-14 15:25
分享是种美德,支持个




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2