欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java_127_容器_表格数据存储_map/list_javabean和list结合存储

程序员文章站 2022-06-17 08:10:29
...

表格数据存储:map/list

package Test;

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 pmc
 *
 */
public class TableTest {
	public static void main(String[] args){
		Map<String,Object> row=new HashMap<String,Object>();
		row.put("id", 1001);
		row.put("name", "张三");
		row.put("薪水", 20000);
		row.put("入职日期", "2018.5.5");
		
		Map<String,Object> row2=new HashMap<String,Object>();
		row2.put("id", 1002);
		row2.put("name", "李四");
		row2.put("薪水", 30000);
		row2.put("入职日期", "2018.4.4");
		
		Map<String,Object> row3=new HashMap<String,Object>();
		row3.put("id", 1003);
		row3.put("name", "王五");
		row3.put("薪水", 3000);
		row3.put("入职日期", "2020.5.4");
		
		List<Map<String,Object>> table=new ArrayList<Map<String,Object>>();{
		table.add(row);
		table.add(row2);
		table.add(row3);
			for(Map<String,Object> rows:table){
				Set<String> keyset=rows.keySet();
				for(String key:keyset){
					System.out.print(key+":"+rows.get(key)+"\t");
				}
				System.out.println();
			}
		}
		
	}
}

表格数据存储:javabean/list结合存储

package Test;

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

/**
 * 测试表格数据的存储
 * ORM思想:对象关系映射
 * map表示一行数据,多行数据多个map,将多个map放到list中
 * **********************************************
 * 每一行数据使用一个:javabean对象,多行数据放到map,list中。
 * 整个表格使用一个Map/List
 * @author pmc
 *
 */
public class TableTest2 {
	public static void main(String[] args){
		User5 user1=new User5(1001,"张三",2000,"2018.5.5");
		User5 user2=new User5(1002,"李四",4000,"2019.6.5");
		User5 user3=new User5(1003,"王五",6000,"2020.7.5");
		System.out.println("list");
		List<User5> list=new ArrayList<User5>();
		list.add(user1);
		list.add(user2);
		list.add(user3);
		for(User5 u:list){
			System.out.println(u);
		}
		System.out.println("set");
		Set<User5> set=new HashSet<User5>();
		set.add(user1);
		set.add(user2);
		set.add(user3);
		for(User5 u:set){
			System.out.println(u);
		}
		System.out.println("map");
		Map<Object,User5> map=new HashMap<Object,User5>();
		map.put(1,user1);
		map.put(2,user2);
		map.put(3,user3);
		Set<Object> i=map.keySet();	
		for(Object u:i){
			System.out.println(map.get(u));
		}
	}
}





class User5{
	private int id;
	private String name;
	private double salary;
	private String date;
	public User5() {
		super();
	}
	public User5(int id, String name, double salary, String date) {
		super();
		this.id = id;
		this.name = name;
		this.salary = salary;
		this.date = date;
	}
	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 getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", salary=" + salary + ", date=" + date + "]";
	}
}

方便理解后期JDBC的数据库表操作

相关标签: Java_笔记