【JAVA】表格数据的存储(1) 使用Map和List相结合的方式存储整张表格
程序员文章站
2022-06-17 08:46:43
...
package cn.sxt.collection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 表格数据的存储
* map和list结合存储整张表格
* 使用map存储每一行数据,然后用list把多个map存储起来,最后打印
* @author Administrator
*
*/
public class TestStoreData {
public static void main(String[] args) {
Map<String,Object> row1=new HashMap<>();
row1.put("id", 1001);
row1.put("name", "柴一");
row1.put("salary", 6000);
row1.put("入职时间","201906");
Map<String,Object> row2=new HashMap<>();
row2.put("id", 1002);
row2.put("name", "柴二");
row2.put("salary", 3000);
row2.put("入职时间","201901");
Map<String,Object> row3=new HashMap<>();
row3.put("id", 1003);
row3.put("name", "柴三");
row3.put("salary", 9000);
row3.put("入职时间","202008");
List<Map<String,Object>> list=new ArrayList<>();
list.add(row1);
list.add(row2);
list.add(row3);
for(Map<String, Object> row:list) {
Set<String> keyset=row.keySet();
for(String key:keyset) {
System.out.print(key+":"+row.get(key)+"\t");
}
System.out.println();
}
}
}
输出结果:
name:柴一 id:1001 入职时间:201906 salary:6000
name:柴二 id:1002 入职时间:201901 salary:3000
name:柴三 id:1003 入职时间:202008 salary:9000
下一篇: Docker 入门教程