package 表格数据存储;
import java.util.*;
public class TestStoreData {
public static void main(String[] args) {
Map<String,Object>row1=new HashMap<>();//数据不确定类型时使用Object
row1.put("id",1001);
row1.put("姓名","张三");
row1.put("薪水",200000);
row1.put("入职日期","2018.5.5");
Map<String,Object>row2=new HashMap<>();//数据不确定类型时使用Object
row2.put("id",1002);
row2.put("姓名","李四");
row2.put("薪水",300000);
row2.put("入职日期","2005.4.1");
Map<String,Object>row3=new HashMap<>();//数据不确定类型时使用Object
row2.put("id",1003);
row2.put("姓名","王五");
row2.put("薪水",3000);
row2.put("入职日期","2020.5.4");
List<Map<String,Object>>table =new ArrayList<>();
table.add(row1);
table.add(row2);
table.add(row3);
for(Map<String,Object>row:table){
Set<String>keyset=row.keySet();
for(String key: keyset){
System.out.print(key+":"+row.get(key)+"\t");
}
System.out.println();
}
}
}
package 表格数据存储;
import java.util.*;
//每一行数据使用javabean对象存储 多行使用放到map或list中
public class TestStoreData2 {
public static void main(String[] args) {
User user1=new User(1001,"张三",2000,"2018.5.6");
User user2=new User(1002,"李四",30000,"2005.5.6");
User user3=new User(1003,"王五",3000,"2020.5.6");
List<User>list=new ArrayList<>();
list.add(user1);
list.add(user2);
list.add(user3);
for(User u:list){
System.out.println(u);
}
Map<Integer,User>map=new HashMap<>();
map.put(1001,user1);
map.put(1002,user2);
map.put(1003,user3);
Set<Integer>keyset = map.keySet();
for(Integer key:keyset){
System.out.println(key+"---"+map.get(key));
}
}
}
//一个完整的javabean 要有set和get方法 以及一个无参构造器
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;
}
@Override
public String toString() {
return "id:"+name+",salary:"+salary+",hiredate:"+hiredate;
}
}
文章评论