使用容器存储表格数据

使用容器存储表格数据

使用容器存储以下表格数据。

ID 姓名 薪水 入职日期
1001 张三 20000 2018.5.5
1002 李四 30000 2005.5.4
1003 王五 3000 2020.5.4

Map和List结合存储

Map表示一行数据,多行数据是多个Map,将多个Map放到List中。代码如下:

package com.msl.collection;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 测试表格数据的存储
 * ORM思想的简单实验:map表示一行数据,多行数据是多个map;将多个map放到list中
 * @author Senley
 *
 */
public class TestStoreData {
    public static void main(String[] args) {
        Map<String,Object> row1 = new HashMap<>();
        row1.put("id", 1001);
        row1.put("姓名", "张三");
        row1.put("薪水", 20000);
        row1.put("入职日期", "2018.5.5");
        
        Map<String,Object> row2 = new HashMap<>();
        row2.put("id", 1002);
        row2.put("姓名", "李四");
        row2.put("薪水", 30000);
        row2.put("入职日期", "2005.4.4");
        
        Map<String,Object> row3 = new HashMap<>();
        row3.put("id", 1003);
        row3.put("姓名", "王五");
        row3.put("薪水", 3000);
        row3.put("入职日期", "2020.5.4");
        
        List<Map<String,Object>> table1 = new ArrayList<>();
        table1.add(row1);
        table1.add(row2);
        table1.add(row3);
        
        for(Map<String,Object> row:table1) {
            Set<String> keyset = row.keySet();
            for(String key:keyset) {
                System.out.print(key+":"+row.get(key)+"\t");
            }
            System.out.println();
        }
    }
}

结果如下:

姓名:张三    薪水:20000    id:1001    入职日期:2018.5.5    
姓名:李四    薪水:30000    id:1002    入职日期:2005.4.4    
姓名:王五    薪水:3000    id:1003    入职日期:2020.5.4    

JavaBean和List结合存储

每一行数据使用JavaBean对象存储,多行使用放到Map或List中。代码如下:

package com.msl.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 Senley
 *
 */
public class TestStoreData2 {
    public static void main(String[] args) {
        User user1 = new User(1001,"张三",20000,"2018.5.5");
        User user2 = new User(1002,"李四",30000,"2005.5.4");
        User user3 = new User(1003,"王五",3000,"2020.5.4");
        
        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));
        }
    }
}

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+",name:"+name+",salary:"+salary+",hiredate:"+hiredate;
    }
}

结果如下:

id:1001,name:张三,salary:20000.0,hiredate:2018.5.5
id:1002,name:李四,salary:30000.0,hiredate:2005.5.4
id:1003,name:王五,salary:3000.0,hiredate:2020.5.4
1001====id:1001,name:张三,salary:20000.0,hiredate:2018.5.5
1002====id:1002,name:李四,salary:30000.0,hiredate:2005.5.4
1003====id:1003,name:王五,salary:3000.0,hiredate:2020.5.4
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2021-2022 Senley
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信