Map练习

创建Map集合,创建Emp对象,将创建的Emp对象添加到集合中(Emp对象的id作为Map集合的键)

并将id=005的对象从集合中移除

package com.hanqi.jh;

public class Emp {
    private String id;
    private String name;
    
    
    public String getId() {
        return id;
    }


    public void setId(String id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public Emp(String id,String name)
    {
        this.id=id;
        this.name=name;
    }
    
    
    
    
    

}
package com.hanqi.jh;

import java.util.*;

public class Homework0530 {

    public static void main(String[] args) {
        
        
        Map<String,String> map=new HashMap<>();
        Emp emp1=new Emp("001","张三");
        Emp emp2=new Emp("002","李四");
        Emp emp3=new Emp("003","王五");
        Emp emp4=new Emp("004","赵六");
        Emp emp5=new Emp("005","周七");
        Emp emp6=new Emp("006","吴八");
        
        map.put(emp1.getId(), emp1.getName());
        map.put(emp2.getId(), emp2.getName());
        map.put(emp3.getId(), emp3.getName());
        map.put(emp4.getId(), emp4.getName());
        map.put(emp5.getId(), emp5.getName());
        map.put(emp6.getId(), emp6.getName());
                                
        map.remove(emp5.getId());
        
        
        System.out.println("输出清除id=005后的Map集合");
        
        Set<String> keys=map.keySet();
        
        Iterator<String> it=keys.iterator();
        while(it.hasNext())
        {
            String str=it.next();
            
            System.out.println(str+"="+map.get(str));
        }
        
        
        
        
        
        
        
        

    }

}

运行结果:

原文地址:https://www.cnblogs.com/miss123/p/5541709.html