java集合練習:創建Map集合,創建Emp對象,並將創建的Emp對象添加到集合中


package com.jihe;

public class Emp {
    private String e_id;
    private String e_name;
    public Emp(String e_id, String e_name) {
        super();
        this.e_id = e_id;
        this.e_name = e_name;
    }
    public String getE_id() {
        return e_id;
    }
    public void setE_id(String e_id) {
        this.e_id = e_id;
    }
    public String getE_name() {
        return e_name;
    }
    public void setE_name(String e_name) {
        this.e_name = e_name;
    }
    

}
package com.jihe;
import java.util.*;
public class EmpMap {

    public static void main(String[] args) {
        Map<String,String> map=new HashMap();
        Emp emp=new Emp("001","張三");
        Emp emp1=new Emp("005","李四");
        Emp emp2=new Emp("003","王五");
        Emp emp3=new Emp("008","趙六");
        map.put(emp.getE_id(), emp.getE_name());
        map.put(emp1.getE_id(), emp1.getE_name());
        map.put(emp2.getE_id(), emp2.getE_name());
        map.put(emp3.getE_id(), emp3.getE_name());
        Set<String> keys=map.keySet();
        System.out.println("遍歷集合map:");
        Iterator<String> it=keys.iterator();
        while(it.hasNext())
        {
            String key=it.next();
            System.out.println(key+" "+map.get(key));
        }
        System.out.println("移除的是"+map.remove("005"));
        Map<String,String>treemap=new TreeMap();
        treemap.putAll(map);
        Set set=treemap.keySet();
        System.out.println("遍歷集合treemap:");
        Iterator<String> ss=set.iterator();
        while(ss.hasNext())
        {
            String str=ss.next();
            System.out.println(str+" "+treemap.get(str));
            
        }

    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM