HashMap代码示例


package com.shushine.framework.第七章Java标准类库;

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

/**
 *
 * <p>
 * 描述该类情况 {@link 代表跟谁有关系}
 * </p>
 *
 * @author 王超
 * @since 1.0
 * @date 2016年10月24日 下午8:56:28
 * @see 新建|修改|放弃
 * @see com.shushine.framework.第七章Java标准类库.HashMapDemo
 *      程序开始创建一个散列映射,然后将名字到账户值的映射添加到HashMap中。
 *      接下来,映射的内容使用entrySet()而获得集合的“视图"而显示出来。关键字和值通过
 *      调用由Map.Entry定义的getKey()和getValue()方法而显示。 遍历hashMap必须用迭代器Iterator
 */

public class HashMapDemo {
    public static void main(String[] args) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        // 添加元素
        map.put("Join", new Integer(96));
        map.put("Tom", new Integer(97));
        map.put("Jane", new Integer(98));
        map.put("Hall", new Integer(99));
        // 得到映射的集合
        Set<Entry<String, Object>> set = map.entrySet();
        // 得到迭代器,并使用泛型特性
        Iterator<Entry<String, Object>> i = set.iterator();
        // 遍历
        while (i.hasNext()) {
            Entry<String, Object> me = i.next();
            System.out.println(me.getKey() + ": " + me.getValue());
        }
    }
}

posted @ 2016-10-24 22:04  qingtianBKY  阅读(355)  评论(0编辑  收藏  举报