再练
来源:互联网 发布:诲汝知之乎的读音 编辑:程序博客网 时间:2024/04/24 17:18
import java.util.*;//。
import java.util.Map.Entry;
class Student implements Comparable <Student>
/*定义一个Student类覆写hashCode和equals
方法使其具有比较性继承Comparable接口实现compaeTo方法使其具有一定的排序性*/
{
//定义三个私有成员
private String name;
private int num;
private String addrees;
Student (String name,int num,String addrees)//构造函数
{
this.name=name;
this.num=num;
this.addrees=addrees;
}
public int compareTo(Student s)//实现Comparable接口中的compareTo方法
{
int a= this.name.compareTo(s.name);
if (a==0)
return new Integer(this.num).compareTo(new Integer(s.num));
return a;
}
public String getName()
{
return name;
}
public int getNum()
{
return num;
}
public String getAddrees()
{
return addrees;
}
public int hashCode()//覆写hashCode方法
{
return name.hashCode()+num*34;
}
public boolean equals(Object obj)//覆写equals方法
{
if (!(obj instanceof Student))
{
throw new ClassCastException("失败");
}
Student s=(Student)obj;
return this.name.equals(s.name)&&this.num==s.num&&this.addrees.equals(s.addrees);
}
public String toString()
{
return "学号: "+num+" 姓名: "+name+" 地址: "+addrees;
}
}
public class MapTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<Student,Integer> map=new HashMap<Student,Integer>();
map.put(new Student("Tom",001,"Baijing"), 53);
map.put(new Student("Jam",002,"Baijing"), 87);
map.put(new Student("Joy",003,"Baijing"), 96);
map.put(new Student("Pak",004,"Baijing"), 49);
//第一种取出方式 keySet方法
Set<Student> keyset=map.keySet();
Iterator it =keyset.iterator();
while (it.hasNext())
{
Student st=(Student) it.next();
Integer score=map.get(st);
System.out.println(st+":::"+score);
}
//第二种取出方式 entrySet方法
Set<Map.Entry<Student,Integer>> entryset=map.entrySet();
Iterator<Map.Entry<Student,Integer>> its =entryset.iterator();
while (its.hasNext())
{
Map.Entry<Student,Integer> mm=its.next();
Student stu= mm.getKey();
Integer sco=mm.getValue();
System.out.println(stu+" 成绩: "+sco);
}
}
}
- 再练
- 字符串反序再练
- hdu2717&&hdu1548-再练BFS
- 练习-再练switch语句
- 统计单词数量(再练)
- 百练_4044:小白鼠再排队
- 练腹肌
- 练练笔
- 练说话
- 练琴
- 练声
- 练声
- 练猫步
- dp 杂练/专练
- dp 杂练/专练 round2
- 正在练和弦..
- 练习用的
- 大家一起练小人!
- helloworld驱动程序
- C++0X的三件好东西(零)
- httpclient 模拟登录人人网, 发心情(刷屏)
- 拉姆齐定理
- 在ASP.NET中配置FCKeditor【带word导入功能】
- 再练
- timer的interval值最大到底是多少
- /etc/init.d/rcS内容分析
- SQL2008数据库导入到SQL2005中
- Mysql 数据库操作类
- HTTP GET和POST的区别
- Google发布Chrome Web Store应用商店
- MySQL性能优化的最佳21条经验
- (转载)个人对Chrome Web Store的一点想法