码迷,mamicode.com
首页 > 其他好文 > 详细

Map集合的四种遍历方式

时间:2017-06-14 19:20:10      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:com   nbsp   iter   ring   int   out   st3   span   sys   

package com.xiaowu.map.demo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.Test;

/**
 * 
 * Map集合的四种遍历方式
 * 
 * 
 * @author WQ
 *
 */
public class MapDemo {

    @Test
    public void Test() {
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < 10; i++) {
            map.put("key" + i, "value" + i);
        }
        for (String i : map.keySet()) {
            Object object = map.get(i);
            System.out.println("object:" + object);
        }
    }

    @Test
    public void Test1() {
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < 10; i++) {
            map.put("key" + i, "value" + i);
        }
        Iterator<Entry<String, Object>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, Object> next = it.next();
            System.out.println(next.getKey() + "---------" + next.getValue());
        }
    }

    @Test
    public void Test2() {
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < 10; i++) {
            map.put("key" + i, "value" + i);
        }
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            System.out.println("key= " + entry.getKey() + " and value= "
                    + entry.getValue());
        }
    }

    @Test
    public void Test3() {
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < 10; i++) {
            map.put("key" + i, "value" + i);
        }
        for (Object v : map.values()) {
            System.out.println("value= " + v);
        }
    }

}

 

Map集合的四种遍历方式

标签:com   nbsp   iter   ring   int   out   st3   span   sys   

原文地址:http://www.cnblogs.com/mr-wuxiansheng/p/7010459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!