两种不同存储方法
package collection02;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.text.html.HTMLDocument.Iterator;
public class Test02 {
public static void main(String[] args){
Map map = new HashMap();
map.put("id", 01);
map.put("name", "张伟");
map.put("salary", 3000);
Map map2 = new HashMap();
map2.put("id", 02);
map2.put("name", "老王");
map2.put("salary", 5000);
Map map3 = new HashMap();
map3.put("id", 03);
map3.put("name", "小二");
map3.put("salary", 6000);
List list = new ArrayList();
list.add(map);
list.add(map2);
list.add(map3);
printName(list);
}
public static void printName(List<Employee> list){
for(int i=0;i<list.size();i++){
Map temp = (Map) list.get(i);
System.out.println(temp.get("id")+"---"+temp.get("name"));
}
}
}
package collection02;
import java.util.ArrayList;
import java.util.List;
public class Test01 {
public static void main(String[] args){
Employee e = new Employee(01, "张伟", 5000, "项目部", "2017-07");
Employee e2 = new Employee(02, "张话", 1000, "技术目部", "2007-07");
Employee e3 = new Employee(03, "老王", 8000, "ceo", "1995-07");
List<Employee> list = new ArrayList<Employee>();
list.add(e);
list.add(e2);
list.add(e3);
printName(list);
}
public static void printName(List<Employee> list){
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).getName());
}
}
}
package collection02;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//一个类对应一个表结构
public class Employee {//javabean 实体类
private int id;
private String name;
private int salary;
private String department;
private Date hiredate;
public Employee(int id, String name, int salary, String department, String hiredate) {
super();
this.id = id;
this.name = name;
this.salary = salary;
this.department = department;
DateFormat df = new SimpleDateFormat("yyyy-MM");//字符串转为时间格式
try {
this.hiredate = df.parse(hiredate);
} catch (ParseException e) {
e.printStackTrace();
}
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
}