Map集合的基本概念

Map集合用于存储元素对(即存储的是一对键值),是通过key映射到它的键值value。

public class Test3 {
    public static void main(String[] args) {
        Map m = new HashMap();
        m.put("aaa",new Student(1232,"张三"));
        m.put("baa",new Student(1232,"王五"));
        m.put("caa",new Student(1232,"赵四"));

        //取value
        System.out.println(m.get("aaa"));

        //取key和value
        Set s = m.entrySet();
        Iterator iter = s.iterator();
        while(iter.hasNext()){
            Map.Entry entry = (Map.Entry) iter.next();
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

        //取所有的值
        Collection c = m.values();
        Iterator it = c.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }

        //取所有的键
        Set ss = m.keySet();
        Iterator iter1 = ss.iterator();
        while(iter1.hasNext()){
            System.out.println(iter1.next());
        }
    }
}

输出结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44084434/article/details/91413905