java从入门到精髓 - 集合类HashMap
时间:2010-08-31 来源:叮当小马
import java.util.*; //导入类
public class MapTest {
/**
* @param args
*/
public static void main(String[] args){
Map map = new HashMap();
Emp emp = new Emp("001","张三");
Emp emp2 = new Emp("005","张四"); //创建emp对象
Emp emp3 = new Emp("004","王一");
map.put(emp.getE_id(), emp.getE_name());
map.put(emp2.getE_id(), emp2.getE_name());
map.put(emp3.getE_id(), emp3.getE_name());
Set set = map.keySet();
Iterator it = set.iterator();
System.out.println("HashMap类:无序");
while(it.hasNext()) //遍历set
{
String str = (String)it.next();
String name = (String)map.get(str);
System.out.println(str + " "+ name);
}
TreeMap treemap = new TreeMap();
treemap.putAll(map);
Iterator iter = treemap.keySet().iterator();
System.out.println("treemap类map集合升序:");
while(iter.hasNext())
{
String str = (String)iter.next();
String name = (String)map.get(str);
System.out.println(str+ " "+ name);
}
}
}
class Emp
{
private String e_id;
private String e_name;
public Emp(String e_id,String e_name)
{
this.e_id = e_id;
this.e_name =e_name;
}
/**
* @return the e_id
*/
protected String getE_id() {
return e_id;
}
/**
* @param e_id the e_id to set
*/
protected void setE_id(String e_id) {
this.e_id = e_id;
}
/**
* @return the e_name
*/
protected String getE_name() {
return e_name;
}
/**
* @param e_name the e_name to set
*/
protected void setE_name(String e_name) {
this.e_name = e_name;
}
}