2018-12-19作业
第一题:
(Map)已知某学校的教学课程内容安排如下:
完成下列要求:
1) 使用一个Map,以老师的名字作为键,以老师教授的课程名作为值,表示上述
课程安排。
2) 增加了一位新老师Allen 教JDBC
3) Lucy 改为教CoreJava put方法
4) 遍历Map,输出所有的老师及老师教授的课程
5) 利用Map,输出所有教JSP 的老师
public class test1 {
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");
System.out.println(map.toString());
map.put("Allen", "JDBC");
System.out.println(map.toString());
map.put("Lucy", "CoreJava");
System.out.println(map.toString());
Set<Entry<String, String>> entrySet = map.entrySet();
System.out.println(entrySet);
for(String name:map.keySet()) {
if(map.get(name).equals("JSP")) {
System.out.println(name);
}
}
/*for(String name:map.keySet()){
System.out.println(name);
System.out.println(map.get(name));
}*/
/*Set<Entry<String,String>> entrySet2 = map.entrySet();
for(Entry<String,String>e:entrySet) {
System.out.println(e.getKey()+","+e.getValue());
}*/
第二题:
(Map)设计Account 对象如下:(有三个属性,long id,double balance,String password)
要求完善设计,使得该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 和余额。
package test2;
import java.util.Date;
import java.util.Random;
public class Account {
Random input=new Random();
private Long id;
private double balance;
private String password;
public Account(double balance, String password) {
this.id=new Date().getTime()+input.nextInt();
this.balance = balance;
this.password = password;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(balance);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + (int) (id ^ (id >>> 32));
result = prime * result + ((password == null) ? 0 : password.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Account other = (Account) obj;
if (Double.doubleToLongBits(balance) != Double.doubleToLongBits(other.balance))
return false;
if (id != other.id)
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
return true;
}
@Override
public String toString() {
return "Account [id=" + id + ", balance=" + balance + ", 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 Account(long id, double balance, String password) {
super();
this.id = id;
this.balance = balance;
this.password = password;
}
public Account() {
super();
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class test1 {
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<>();
for (Account account : list) {
map.put(account.getId(), account);
}
System.out.println(map.entrySet());
}
}