当前位置:网站首页>Using containers to store table data

Using containers to store table data

2020-11-08 21:08:00 8Years

Method 1. The data used for storing rows is map. There will be more map In the list Inside .( The container can hold anything )

public class TestStoreData {

    public static void main(String[] args) {
        Map<String,Object> row1=new HashMap<>();
        row1.put("id",1001);
        row1.put(" full name "," Zhang San ");
        row1.put(" salary ",200);
        row1.put(" Date of entry ","2009");





        Map<String,Object> row2=new HashMap<>();
        row2.put("id",1002);
        row2.put(" full name "," Zhao si ");
        row2.put(" salary ",50);
        row2.put(" Date of entry ","2010");


        List<Map<String,Object>> table=new ArrayList<>();
        table.add(row1);
        table.add(row2);
        for (Map<String,Object> row:table) {
            Set<String> keyset=row.keySet();
            for (String key:keyset) {
                System.out.print(key+row.get(key)+"   ");

            }
            System.out.println();

        }


    }

}


 Output results 
 Name Zhang San     salary 200   id1001    Date of entry 2009   
 Name: Zhao Si     salary 50   id1002    Date of entry 2010 

 

Method 2: Use Javabean and list Store the entire table

public class TestStoreDatatwo {
    public static void main(String[] args) {
        Stu stu1=new Stu(1,"li",100.0);
        Stu stu2=new Stu(2,"li",100.0);
        List<Stu> list=new ArrayList<>();
        list.add(stu1);
        list.add(stu2);
        for (Stu stu:list) {
            System.out.println(stu);

        }
    }
}


class Stu {
    int id;
    String name;
    Double salary;


    public Stu() {
    }

    public Stu(int id, String name, Double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    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;
    }
    
    @Override
    public String toString() {
        return "id:" + id + ",name:" + name + ",salary:" + salary;
    }
}

Be careful :JavaBean Want to have      1.getter,setter Method     2. Constructors .3. Parameterless constructors , And there are toString Method

版权声明
本文为[8Years]所创,转载请带上原文链接,感谢