2018/12/24

2018/12/24

1)使用一个Map,以老师的名字作为键,以老师教授的课程名作为值,表示上述 课程安排。
2)增加了一位新老师Allen 教JDBC
3) Lucy 改为教CoreJava put方法
4)遍历Map,输出所有的老师及老师教授的课程
5)利用Map,输出所有教JSP 的老师。

public class Practice3_1 {
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");
	map.put("Allen", "JDBC");
	map.put("Lucy", "CoreJava");
	Set<Entry<String, String>> entrySet = map.entrySet();
	for(Entry<String, String> e:entrySet) {
		if(e.getValue().equals("Jsp")) {
			String key = e.getKey();
			System.out.println(key+" ");
			System.out.println();
		}
		System.out.println(e.getKey()+"\t"+e.getValue());	
	}
	}
}

要求完善设计,使得该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 class Pratice3_2 {
public static void main(String[] args) {
	List<Account> list = new ArrayList<>();
	list.add(new Account(10.00, "1234"));
	list.add(new Account(15.00, "5678"));
	list.add(new Account(0, "1010"));
	
	HashMap<Long, Account> map = new HashMap<>();
	


	Random y = new Random();
	for(int i=0;i<list.size();i++) {
		
		map.put(new Date().getTime()+y.nextInt(10), list.get(i));
	}
	
	
	
	Set<Entry<Long, Account>> entrySet = map.entrySet();
	for(Entry<Long, Account> e:entrySet) {
		System.out.println(e.getKey()+"\t"+e.getValue());
	}
	
}
}
class Account{
	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;
	}
	private long id;
	private double balance;
	public Account() {
		super();
	}
	public String toString() {
		return "Account [id=" + id + ", balance=" + balance + ", password=" + password + "]";
	}
	public Account(double balance, String password) {
		super();
		Random x = new Random();
		this.id = new Date().getTime()+x.nextInt(10);
		this.balance = balance;
		this.password = password;
	}
	private String password;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值