1.编写一个程序,创建一个 HashMap对象,用于存储银行储户的信息(其中储户的主要信息有储户的ID,姓名和余额)。另外,计算并显示其中某个储户的当前余额。
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class Test1
{
public static void main (String []args)
{
User u1=new User(2001,"哈哈",15000);
User u2=new User(2002,"娃哈哈",16000);
User u3=new User(2003,"笑哈哈",5000);
User u4=new User(2004,"乐哈哈",17000);
HashMap<Integer,User> m=new HashMap<Integer,User>();
m.put(2001, u1);
m.put(2002, u2);
m.put(2003, u3);
m.put(2004, u4);
for(Entry<Integer,User> entry:m.entrySet())
{
System.out.println(entry.getValue());
}
}
}
class User implements Comparable
{
String name;
int id;
int money;
public User(int id,String name, int money)
{
this.name=name;
this.id=id;
this.money=money;
}
public String toString()
{
return this.name+this.id+this.money;
}
public int compareTo(Object o)
{
User u=(User) o;
if(this.id-u.id>0)
{
return 1;
}
if (this.id-u.id==0)
{
return this.name.compareTo(u.name);
}
return -1;
}
}