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

表格数据存储_javabean和List存储整张表

程序员文章站 2022-06-17 08:04:35
...
Id 名字 薪水
1001 阿狸 210236
1002 阿卡丽 993256
1003 瑞雯 714226

javabean的构建

一个完整的javabean要有set,get方法和无参构造器

class User{
	private int id;
	private String name;
	private int salary;
	public User(){
	}
	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 int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public String toString() {
		return "id:"+id+",name:"+name+",salary:"+salary;
	}
		public User(int id, String name, int salary) {
		super();
		this.id = id;
		this.name = name;
		this.salary = salary;
	}
}

用List存储javabean构成表格

		User u1 = new User(1001,"阿狸",210236);
		User u2 = new User(1002,"阿卡丽",993256);
		User u3 = new User(1003,"瑞雯",714226);
		List <User> l1 = new ArrayList();
		l1.add(u1);
		l1.add(u2);
		l1.add(u3);
		for(User u:l1) {
			System.out.println(u);
		}

用Map存储javabean构成表格

		Map <Integer,User> m1 = new HashMap();
		m1.put(1001, u1);
		m1.put(1002, u2);
		m1.put(1003, u3);
//
		Set <Integer> keyset = m1.keySet();
		for(Integer key:keyset) {
			System.out.println(m1.get(key));
		}

所有代码

package test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/*
 * 
 * 表格数据存储
 * 使用List存储Javabean_使用Map存储Javabean
 * 
 */
public class TestStoreData2 {
	public static void main(String[] args) {
		User u1 = new User(1001,"阿狸",210236);
		User u2 = new User(1002,"阿卡丽",993256);
		User u3 = new User(1003,"瑞雯",714226);
//		使用List存储javabean
		List <User> l1 = new ArrayList();
		l1.add(u1);
		l1.add(u2);
		l1.add(u3);
		for(User u:l1) {
			System.out.println(u);
		}
//		使用Map存储javabean
		Map <Integer,User> m1 = new HashMap();
		m1.put(1001, u1);
		m1.put(1002, u2);
		m1.put(1003, u3);
//
		Set <Integer> keyset = m1.keySet();
		for(Integer key:keyset) {
			System.out.println(m1.get(key));
		}
	}
}
//一个完整的javabean要有set,get方法和无参构造器
class User{
	private int id;
	private String name;
	private int 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 int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public User() {
		
	}
	public String toString() {
		return "id:"+id+",name:"+name+",salary:"+salary;
	}
	public User(int id, String name, int salary) {
		super();
		this.id = id;
		this.name = name;
		this.salary = salary;
	}
}

效果
表格数据存储_javabean和List存储整张表