当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
猜你喜欢

JVM Zhenxiang series: easy understanding of class files to virtual machines (Part 2)

Dynamic planning

【云服务】阿里云服务器ECS实例规格那么多,如何选型?最佳实践说明

线程池运用不当的一次线上事故

The interface testing tool eolinker makes post request

Fiddler无法正常抓取谷歌等浏览器的请求_解决方案

Django之简易用户系统(3)

. net core cross platform resource monitoring library and dotnet tool

Summary of interface test case ideas

Solve the problem that the value of new date() of JS in IE and Firefox is invalid date and Nan Nan
随机推荐
动态规划设计:最大子数组
解决IE、firefox浏览器下JS的new Date()的值为Invalid Date、NaN-NaN的问题
Flink的DataSource三部曲之三:自定义
综合架构的简述
数组初相识
An online accident caused by improper use of thread pool
Looking for a small immutable dictionary with better performance
使用基于GAN的过采样技术提高非平衡COVID-19死亡率预测的模型准确性
ITerm2 配置和美化
Case analysis of entitycore framework
接口测试用例思路总结
Countdownlatch explodes instantly! Based on AQS, why can cyclicbarrier be so popular?
What courses will AI programming learn?
中缀表达式转后缀表达式
Problem solving templates for subsequence problems in dynamic programming
采用注解+拦截器的方式进行异步执行的实现方式
VirtualBox install centos7
Test comparison of three domestic cloud databases
Iterm2 configuration and beautification
Creating a text cloud or label cloud in Python