java300集-容器-数据存储综合练习

List-Arraylist-Linkedlist(双向链表)
Map-hashmap-equals和hashcode
set-HashSet
iterator
泛型

下面实现下图中的表格:
在这里插入图片描述

package cn.bjsxt.collection2;

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 format = new SimpleDateFormat("yyyy-MM");
		try {
			this.hireDate = format.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;
	}  
	
	

}

javabean的介绍

package cn.bjsxt.collection2;

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

public class Test01 {


	public static void main(String[] args) throws Exception { 
		//一个对象对应了一行记录!
		Employee e = new Employee(0301,"高琪",3000,"项目部","2007-10");
		Employee e2 = new Employee(0302,"马士兵",3500,"教学部","2006-10");
		Employee e3 = new Employee(0303,"裴新",3550,"教学部","2006-10");
		
		List<Employee> list = new ArrayList<Employee>();
		
		list.add(e);
		list.add(e2);
		list.add(e3);
		
		printEmpName(list);
		
	}

	public static void printEmpName(List<Employee> list){
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i).getName());
		}
	}
	
	
}

map保存表记录

package cn.bjsxt.collection2;

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

public class Test02 {

	public static void main(String[] args) {
		//一个map对象对应一行记录!!!
		Map map = new HashMap();
		map.put("id", 0301);
		map.put("name", "高琪");
		map.put("salary", 3050);
		map.put("department","项目部");
		map.put("hireDate", "2007-10");  
		
		Map map2 = new HashMap();
		map2.put("id", 0302);
		map2.put("name", "马士兵");
		map2.put("salary", 3500);
		map2.put("department","教学部");
		map2.put("hireDate", "2006-10");  
		
		Map map3 = new HashMap();
		map3.put("id", 0302);
		map3.put("name", "裴新");
		map3.put("salary", 3500);
		map3.put("department","教学部");
		map3.put("hireDate", "2006-10");  
		
		
		List<Map> list = new ArrayList<Map>();
		
		list.add(map);
		list.add(map2);
		list.add(map3);
		
		printEmpName(list);
		
	}
	
	public static void printEmpName(List<Map> list){
		for(int i=0;i<list.size();i++){
			Map tempMap = list.get(i);
			System.out.println(tempMap.get("name")+"--"+tempMap.get("salary"));//注意这里name和salary是定义的键,要用双引号!!
		}
	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值