【JAVA】表格数据的存储(2)每一行使用javabean对象存储,多行使用放到map或者list中
程序员文章站
2022-06-17 08:06:11
...
package cn.sxt.collection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 测试表格数据的存储
* 体会ORM思想
* 每一行使用javabean对象存储,多行使用放到map或者list中
* @author Administrator
*
*/
public class TestStoreData02 {
public static void main(String[] args) {
User user1=new User(1001,"柴一",6000,"202008");
User user2=new User(1002,"柴二",9000,"201904");
User user3=new User(1003,"柴三",7000,"201807");
List<User> list=new ArrayList<>();//使用list
list.add(user1);
list.add(user2);
list.add(user3);
for(User user:list) {
System.out.println(user);
}
System.out.println();
System.out.println();
///
Map<Integer,User> map=new HashMap<>();//使用map
map.put(1001, user1);
map.put(1002, user2);
map.put(1003, user3);
Set<Integer> keyset=map.keySet();
for(Integer key:keyset) {
System.out.println(map.get(key));
}
}
}
class User{
private int id;
private String name;
private double salary;
private String hiredate;
//一个完整的javabean,要有set和get方法,以及一个无参构造器
public User() {
}
public User(int id, String name, double salary, String hiredate) {
super();
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+"\t"+"name:"+name+"\t"+"salary:"+salary+"\t"+"入职时间:"+hiredate;
}
}
输出结果:
id:1001 name:柴一 salary:6000.0 入职时间:202008
id:1002 name:柴二 salary:9000.0 入职时间:201904
id:1003 name:柴三 salary:7000.0 入职时间:201807
id:1001 name:柴一 salary:6000.0 入职时间:202008
id:1002 name:柴二 salary:9000.0 入职时间:201904
id:1003 name:柴三 salary:7000.0 入职时间:201807
上一篇: 用python将word文档合并实例代码