最近在看JAVA教学的视频,觉得老师讲的很好,同时借用源代码还有笔记来撰写本系列博客,记录自己的学习内容,同时也供看到的人学习。
本篇第一个内容是模拟实现HandSet,其实这个模拟和之前的相比简陋了很多,因为Set的源码是比较复杂的,而且Set很大一部分是依赖于Map的,所以模拟的代码量并不多:
import java.util.HashMap;
/**
* 自定义自己的HashSet,Set里面存放的相当于Map里面的key值,而真正的value则放在Map里面
*/
public class SxtHashSet {
HashMap map; //用来存放真实的元素信息
private static final Object PRESENT = new Object();
public SxtHashSet(){
map = new HashMap(); //参考源码
}
public int size(){
return map.size();
}
public void add(Object o){
map.put(o, PRESENT); //set的不可重复就是利用了map里面键对象的不可重复!
}
public static void main(String[] args) {
SxtHashSet s = new SxtHashSet();
s.add("aaa");
s.add(new String("aaa"));
System.out.println(s.size());
}
}
接下来进行的是一个小练习,利用所学知识进行一个数据存储的练习,设计的数据表格如下:
两种解决方式,第一种是用javabean(一种常见的数据存储容器,利用构造器来进行数据导入与存储,JSP开发里面经常用到):
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;
//利用Eclipse自动添加构造器“Generate Constructor using Fields”选项
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;
}
}
import java.awt.print.Printable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Test01 { //用JavaBean的方法来进行数据存储
public static void main(String[] args) {
//加上合适的构造器之后,一行对应一条记录
Employee e = new Employee(0301,"gaoqi",3000,"xiangmubu","2007-10");
Employee e2 = new Employee(0302,"mashibing",3000,"jiaoxuebu","2006-10");
Employee e3 = new Employee(0303,"peixin",3000,"jiaoxuebu","2007-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容器来存储:
import java.util.ArrayList;
import java.util.HashMap;
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", "gaoqi");
map.put("salary", 3050);
map.put("department", "xiangmubu");
map.put("hireDate", "2007-10");
Map map2 = new HashMap();
map2.put("id", 0302);
map2.put("name", "mashibing");
map2.put("salary", 3050);
map2.put("department", "xiangmubu");
map2.put("hireDate", "2007-10");
Map map3 = new HashMap();
map3.put("id", 0303);
map3.put("name", "peixin");
map3.put("salary", 3050);
map3.put("department", "xiangmubu");
map3.put("hireDate", "2007-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"));
}
}
}