List-Arraylist-Linkedlist(双向链表)
Map-hashmap-equals和hashcode
set-HashSet
iterator
泛型
下面实现下图中的表格:
package cn.bjsxt.collection2;
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 format = new SimpleDateFormat("yyyy-MM");
try {
this.hireDate = format.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;
}
}
javabean的介绍
package cn.bjsxt.collection2;
import java.util.ArrayList;
import java.util.List;
public class Test01 {
public static void main(String[] args) throws Exception {
//一个对象对应了一行记录!
Employee e = new Employee(0301,"高琪",3000,"项目部","2007-10");
Employee e2 = new Employee(0302,"马士兵",3500,"教学部","2006-10");
Employee e3 = new Employee(0303,"裴新",3550,"教学部","2006-10");
List<Employee> list = new ArrayList<Employee>();
list.add(e);
list.add(e2);
list.add(e3);
printEmpName(list);
}
public static void printEmpName(List<Employee> list){
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).getName());
}
}
}
map保存表记录
package cn.bjsxt.collection2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class Test02 {
public static void main(String[] args) {
//一个map对象对应一行记录!!!
Map map = new HashMap();
map.put("id", 0301);
map.put("name", "高琪");
map.put("salary", 3050);
map.put("department","项目部");
map.put("hireDate", "2007-10");
Map map2 = new HashMap();
map2.put("id", 0302);
map2.put("name", "马士兵");
map2.put("salary", 3500);
map2.put("department","教学部");
map2.put("hireDate", "2006-10");
Map map3 = new HashMap();
map3.put("id", 0302);
map3.put("name", "裴新");
map3.put("salary", 3500);
map3.put("department","教学部");
map3.put("hireDate", "2006-10");
List<Map> list = new ArrayList<Map>();
list.add(map);
list.add(map2);
list.add(map3);
printEmpName(list);
}
public static void printEmpName(List<Map> list){
for(int i=0;i<list.size();i++){
Map tempMap = list.get(i);
System.out.println(tempMap.get("name")+"--"+tempMap.get("salary"));//注意这里name和salary是定义的键,要用双引号!!
}
}
}