码迷,mamicode.com
首页 > 其他好文 > 详细

Map code

时间:2017-07-10 10:27:55      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:tor   list   iterator   name   log   next   map   private   void   

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

 

Map code

标签:tor   list   iterator   name   log   next   map   private   void   

原文地址:http://www.cnblogs.com/xuewuzhijing95hao/p/7144121.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!