一:javabean 和list容器或map容器
package cn. wyj. two ; import java. util. * ;
public class Demo13_Javabean 和其他容器 { public static void main ( String [ ] args) { User u1 = new User ( "张三" , 1001 , 5000 , 23 ) ; User u2 = new User ( "李四" , 1002 , 6000 , 24 ) ; User u3 = new User ( "王五" , 1003 , 7000 , 25 ) ; List < User > list = new ArrayList < User > ( ) ; list. add ( u1) ; list. add ( u2) ; list. add ( u3) ; for ( User temp: list) System . out. println ( temp) ; Map < Integer , User > map = new HashMap < Integer , User > ( ) ; map. put ( 1 , u1) ; map. put ( 2 , u2) ; map. put ( 3 , u3) ; Set < Integer > keyset = map. keySet ( ) ; for ( Integer temp: keyset) { System . out. println ( temp+ "-->" + map. get ( temp) ) ; } }
} class User { private String name; private int id; private int slary; private int age; public User ( ) { } public User ( String name, int id, int slary, int age) { super ( ) ; this . name = name; this . id = id; this . slary = slary; this . age = age; } public String getName ( ) { return name; } public void setName ( String name) { this . name = name; } public int getId ( ) { return id; } public void setId ( int id) { this . id = id; } public int getSlary ( ) { return slary; } public void setSlary ( int slary) { this . slary = slary; } public int getAge ( ) { return age; } public void setAge ( int age) { this . age = age; } @Override public String toString ( ) { return "姓名:" + this . name+ " " + "ID:" + this . id+ " " + "薪水:" + this . slary+ " " + "年龄:" + this . age; } }
二:利用map和list容器进行存储数据(map存行 list存入整个表格)
package cn. wyj. two ;
import java. util. * ; public class Demo12_List 和Map 存储一张表 { public static void main ( String [ ] args) { Map < String , Object > m1 = new HashMap < String , Object > ( ) ; m1. put ( "姓名:" , "张三" ) ; m1. put ( "ID:" , 1001 ) ; m1. put ( "薪水:" , 5000 ) ; m1. put ( "年龄:" , 23 ) ; Map < String , Object > m2 = new HashMap < String , Object > ( ) ; m2. put ( "姓名:" , "李四" ) ; m2. put ( "ID:" , 1002 ) ; m2. put ( "薪水:" , 6000 ) ; m2. put ( "年龄:" , 22 ) ; Map < String , Object > m3 = new HashMap < String , Object > ( ) ; m3. put ( "姓名:" , "王五" ) ; m3. put ( "ID:" , 1003 ) ; m3. put ( "薪水:" , 7000 ) ; m3. put ( "年龄:" , 33 ) ; List < Map < String , Object > > list = new ArrayList < Map < String , Object > > ( ) ; list. add ( m1) ; list. add ( m2) ; list. add ( m3) ; for ( Map < String , Object > ss1: list) { Set < String > keyset = ss1. keySet ( ) ; for ( String ss2: keyset) { System . out. print ( ss2+ ss1. get ( ss2) + " " ) ; } System . out. println ( ) ; } } }
储存数据的表格: