The basic concept Map collection

Map elements to be used to store a set of (i.e., stored in a key-value), it is mapped to the key value through the key.

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());
        }
    }
}

Output:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_44084434/article/details/91413905