Java中容器数据简单存储

两种不同存储方法

666

package collection02;

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

import javax.swing.text.html.HTMLDocument.Iterator;

public class Test02 {
    public static void main(String[] args){
        Map map = new HashMap();
        map.put("id", 01);
        map.put("name", "张伟");
        map.put("salary", 3000);

        Map map2 = new HashMap();
        map2.put("id", 02);
        map2.put("name", "老王");
        map2.put("salary", 5000);


        Map map3 = new HashMap();
        map3.put("id", 03);
        map3.put("name", "小二");
        map3.put("salary", 6000);


        List list = new ArrayList();
        list.add(map);
        list.add(map2);
        list.add(map3);
        printName(list);

    }
    public static void printName(List<Employee> list){
        for(int i=0;i<list.size();i++){
            Map temp = (Map) list.get(i);
            System.out.println(temp.get("id")+"---"+temp.get("name"));
            }
    }
}
package collection02;

import java.util.ArrayList;
import java.util.List;

public class Test01 {
    public static void main(String[] args){
    Employee e = new Employee(01, "张伟", 5000, "项目部", "2017-07");
    Employee e2 = new Employee(02, "张话", 1000, "技术目部", "2007-07");
    Employee e3 = new Employee(03, "老王", 8000, "ceo", "1995-07");


        List<Employee> list = new ArrayList<Employee>();
        list.add(e);
        list.add(e2);
        list.add(e3);
        printName(list);
    }




    public static void printName(List<Employee> list){
        for(int i=0;i<list.size();i++){

            System.out.println(list.get(i).getName());
            }
    }

}
package collection02;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//一个类对应一个表结构
public class Employee {//javabean  实体类
    private int id;
    private String name;
    private int salary;
    private String department;
    private Date hiredate;







    public Employee(int id, String name, int salary, String department, String hiredate) {
        super();
        this.id = id;
        this.name = name;
        this.salary = salary;
        this.department = department;

        DateFormat df = new SimpleDateFormat("yyyy-MM");//字符串转为时间格式
        try {
            this.hiredate = df.parse(hiredate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    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 getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    public Date getHiredate() {
        return hiredate;
    }
    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值