Map code

import java.util.Map ;
import java.util.HashMap ;
import java.util.List ;
import java.util.ArrayList ;
import java.util.Iterator ;

class Employee {
    private int id ;
    private String name ;
    
    public void setId(int id) {
        this.id = id ;
    }
    public int getId() {
        return this.id ;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name ;
    }
}


class CreatEmpBean {
    List<Employee> list = new ArrayList<Employee>();
    Map<Integer,Employee>  map = new HashMap<Integer,Employee> ();
    
    public void setEmpList(Employee e){
        list.add(e);
    }
    
    public List<Employee> getEmpList() {
        return list;
    }
    
    public Map<Integer,Employee> getEmpMap(Employee e) {

        map.put(e.getId(),e);
        return map;
    }
}

class PrintEmpBean {
    
    public void printEmpList(List<Employee> list) {
        Iterator it = list.iterator();
        while(it.hasNext()) {
            Employee e1 = (Employee)it.next();
            System.out.println("list :" + e1.getId() + "\t" + e1.getName());
        }
    }
    
    public void printEmpMap(Map<Integer,Employee>  map) {
        Iterator<Map.Entry<Integer,Employee>>  it = map.entrySet().iterator();
        while(it.hasNext()) {
            Map.Entry<Integer,Employee> it1 = it.next();
            System.out.println("map :" + it1.getKey() + " = " + it1.getValue().getId() + "," + it1.getValue().getName() );
        }
    }
}

public class Work {
    public static void main(String[] args) {
        Employee em = new Employee();
        em.setId(1);
        em.setName("王小二");
        
        Employee em1 = new Employee();
        em1.setId(2);
        em1.setName("王小三");
        
        Employee em2 = new Employee();
        em2.setId(3);
        em2.setName("王小四");
        
        CreatEmpBean ce = new CreatEmpBean();
        ce.setEmpList(em) ;
        ce.setEmpList(em1) ;
        ce.setEmpList(em2) ;
        
        ce.getEmpMap(em);
        
        ce.getEmpMap(em);
        ce.getEmpMap(em1);
        ce.getEmpMap(em2);
        
        
        PrintEmpBean pe = new PrintEmpBean();
        pe.printEmpList(ce.getEmpList());
        pe.printEmpMap(ce.getEmpMap(em));
    }
}

 

posted @ 2017-07-10 08:32  Zero豪  阅读(332)  评论(0编辑  收藏  举报