-
import java.util.HashMap;
import java.util.Map;
HomeWork15.java
Map<String, String> m=new HashMap<>();
m.put("Tom", "CoreJava");
m.put("John", "Oracle");
m.put("Susan", "Oracle");
m.put("Jerry", "JDBC");
m.put("Jim", "Unix");
m.put("Kevin", "JSP");
m.put("Lucy", "JSP");
m.put("Allen", "JDBC");
m.put("lucy", "CoreJava");
for (String s : m.keySet()) {
System.out.println(s+"="+m.get(s));
}
System.out.println("-----------------華麗的分割線-----------------");
for (String s : m.keySet()) {
if(m.get(s)=="JSP"){
System.out.println(s);
}
}
-
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
Account.java
private long id;
private double balance;
private String password;
//無參
public Account() {
super();
}
//有參
public Account( double balance, String password) {
super();
Random r=new Random();
this.id =new Date().getTime()+r.nextInt(10);
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;
}
@Override
public String toString() {
return "Account [id=" + id + ", balance=" + balance + ", 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;
}
HomeWork16.java
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"));
Map<Long, Account> m=new HashMap<>();
m.put(list.get(0).getId(), list.get(0));
m.put(list.get(1).getId(), list.get(1));
m.put(list.get(2).getId(), list.get(2));
for (Long a : m.keySet()) {
System.out.println("Id爲:"+a+"\r\n"+"餘額爲:"+m.get(a).getBalance());
}