java22 集合作业 三

#(Map)已知某学校的教学课程内容安排如下:
java22 集合作业 三
完成下列要求:
1) 使用一个Map,以老师的名字作为键,以老师教授的课程名作为值,表示上述
课程安排。
2) 增加了一位新老师Allen 教JDBC
3) Lucy 改为教CoreJava put方法
4) 遍历Map,输出所有的老师及老师教授的课程
5) 利用Map,输出所有教JSP 的老师。

第二题:
(Map)设计Account 对象如下:(有三个属性,long id,double balance,String password)
java22 集合作业 三
要求完善设计,使得该Account 对象能够自动分配id(可考虑使用从1970-1-1 0:0:0以来的毫秒数为id)。
给定一个List 如下:
List list = new ArrayList();
list.add(new Account(10.00, “1234”));
list.add(new Account(15.00, “5678”));
list.add(new Account(0, “1010”));
要求把List 中的内容放到一个Map 中,该Map 的键为id,值为相应的Account 对象。
最后遍历这个Map,打印所有Account 对象的id 和余额。
题一

	public static void main(String[] args) {
		HashMap<String, String> map = new HashMap<>();
		map.put("Tom", "CoreJava");
		map.put("John", "Oracle");
		map.put("Susan", "Oracle");
		map.put("Jerry", "JDBC");
		map.put("Jim", "Unix");
		map.put("Kevin", "JSP");
		map.put("Lucy", "JSP");

//		增加了一位新老师Allen 教JDBC
		map.put("Allen", "JDBC");

//		Lucy 改为教CoreJava   put方法
		map.put("Lucy", "CoreJava");

//		 利用Map,输出所有教JSP 的老师。
		Set<String> set = map.keySet();
		Iterator<String> it = set.iterator();
		while (it.hasNext()) {
			String n = it.next();
			if (map.get(n).equals("JSP")) {
				System.out.println(n);
			}
		}
//		遍历Map,输出所有的老师及老师教授的课程	
		Iterator<String> it2 = set.iterator();
		while (it2.hasNext()) {
			String n2 = it2.next();
			System.out.println(n2 + "," + map.get(n2));
		}

题二

public class Account {

	private Long id = new Date().getTime();
	private Double balance;
	private String password;

	public Account() {
		super();
		// TODO 自动生成的构造函数存根
	}

	public Account(Double balance, String password) {
		super();
		this.balance = balance;
		this.password = password;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Double getBalance() {
		return balance;
	}

	public void setBalance(Double balance) {
		this.balance = balance;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public static void main(String[] args) {
		Account ac1 = new Account(10.00, "1234");
		Account ac2 = new Account(15.00, "5678");
		Account ac3 = new Account(0.00, "1010");
		ArrayList<Account> list = new ArrayList<>();
		list.add(ac1);
		list.add(ac2);
		list.add(ac3);

		HashMap<Long, Account> map = new HashMap<>();
		map.put(ac1.getId() + (long) (Math.random() * 100), ac1);
		map.put(ac2.getId() + (long) (Math.random() * 100), ac2);
		map.put(ac3.getId() + (long) (Math.random() * 100), ac3);

		Set<Long> key = map.keySet();
		Iterator<Long> it = key.iterator();
		while (it.hasNext()) {
			Long n = it.next();
			System.out.println(n + "," + map.get(n).getBalance());
		}

	}