java之表格数据存储
- 摘要
- 表格数据存储
- javabean 介绍
- javabean 设计类
- 表格数据存储
摘要
本博客主要讲述java如何存储表格数据。
表格数据存储
在解决实际问题的时候,需要涉及到如何存储表格的数据,这里讲述了一种使用javabean的方法存储表格
javabean 介绍
javabean 我觉得就是一种规范,规范类的设计,以便更好地解决问题
javabean 设计类
class User{
private int id;
private String name;
private double salary;
private String hiredate;
public User() {
}
public User(int id, String name, double salary, String hiredate) {
this.id = id;
this.name = name;
this.salary = salary;
this.hiredate = hiredate;
}
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 double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getHiredate() {
return hiredate;
}
public void setHiredate(String hiredate) {
this.hiredate = hiredate;
}
@Override
public String toString() {
return "id:" + id + ",name" + name + ",salary" + salary + ",hiredate" + hiredate;
}
}
表格数据存储
public class TestStoreData2 {
public static void main(String[] args) {
User user1 = new User(1001, "张三", 2000, "2018.5.5");
User user2 = new User(1002, "李四", 1000, "2005.5.5");
User user3 = new User(1003, "王五", 20000, "2008.5.5");
Map<Integer, User> map = new HashMap<>();
map.put(1001,user1);
map.put(1002,user2);
map.put(1003,user3);
//获取键的集合
Set<Integer> set = map.keySet();
for(Integer i : set) System.out.println(i + ":" + map.get(i));
}
}
public class TestStoreData2 {
public static void main(String[] args) {
User user1 = new User(1001, "张三", 2000, "2018.5.5");
User user2 = new User(1002, "李四", 1000, "2005.5.5");
User user3 = new User(1003, "王五", 20000, "2008.5.5");
Map<Integer, User> map = new HashMap<>();
map.put(1001,user1);
map.put(1002,user2);
map.put(1003,user3);
//迭代器的做法
Set<Map.Entry<Integer,User>> set = map.entrySet();
for(Iterator<Map.Entry<Integer, User>> iter = set.iterator();iter.hasNext();){
Map.Entry<Integer, User> temp = iter.next();
System.out.println(temp.getKey() + ":" + temp.getValue());
}
}
}