package map;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class HashCode_equals {
public static void main(String[] args) {
HashMap<Student,String> map=new HashMap<Student,String>();
map.put(new Student("张三",22),"北京");
map.put(new Student("李四",212),"北京");
map.put(new Student("李四",212),"北京fff");//注意:健一样,再插入,会覆盖之前的值
map.put(new Student("王五",233),"上海");
map.put(new Student("赵六",2332),"上海");
map.put(new Student("风气",2662),"广州");
//第一种取值方式
Set<Student> set=map.keySet();
Iterator<Student> iter=set.iterator();
while(iter.hasNext()){
Student stu=iter.next();
System.out.println(stu.toString() +",address:"+map.get(stu));//因为复写了这个方法,所以可以从这里取name和age
}//Student.toString(),方法不写的话,也会自动调用的啊
System.out.println("--------------------------------------------------------------");
//第二种取值方式
Set<Map.Entry<Student,String>> set2=map.entrySet();
Iterator<Map.Entry<Student,String>> iter2=set2.iterator();
while(iter2.hasNext()){
Map.Entry<Student,String> entry=iter2.next();
Student stu=entry.getKey();
String str=entry.getValue();
System.out.println(stu +",address:"+str);//因为复写了这个方法,所以可以从这里取name和age
}//Student.toString(),方法不写的话,也会自动调用的啊
System.out.println("============================================================");
//按默认排序从小到大
TreeMap<Student,String> ts=new TreeMap<Student,String> (new MyComparetor());//定义一个比较器,有比较器,按比较器的规则进行比较
ts.put(new Student("aa张三",254542),"北京");
ts.put(new Student("aa张三",212),"北京");
ts.put(new Student("ac李四",212),"北京fff");//注意:健一样,再插入,会覆盖之前的值
ts.put(new Student("dd王五",233),"上海");
ts.put(new Student("ee赵六",2332),"上海");
ts.put(new Student("ea风气",2662),"广州rewqrew");
Set<Map.Entry<Student,String>> treemap=ts.entrySet();
Iterator<Map.Entry<Student,String>> iter_tree=treemap.iterator();
while(iter_tree.hasNext()){
Map.Entry<Student,String> tree_map=iter_tree.next();
System.out.println(tree_map.getKey()+"----address:"+tree_map.getValue());
}
}
}
class MyComparetor implements Comparator<Student>{
public int compare(Student stu1, Student stu2) {
int num=stu1.getName().compareTo(stu2.getName());//线比较姓名
if(num==0){//姓名相同,在比较年龄
return new Integer(stu1.getAge()).compareTo(new Integer(stu2.getAge()));
}
return num;
}
}
/**
* 一:复写hashCode()方法和equals()方法是为了此对象存入Hash结构中定义的新的存入规则
* 二:实现Comparator接口,是为了此对象存入二叉树中定义的新的存入规则
* @author wjw
*
*/
class Student implements Comparable<Student>{
private String name;
private int age;
public Student(String name,int age){
this.name=name;
this.age=age;
}
//实现接口中的方法
public int compareTo(Student stu) {
int num=new Integer(this.age).compareTo(stu.age);//先按照年龄排序
if(num==0){//如果年龄相等
return this.name.compareTo(stu.name);//再按照姓名排序
}
return num;
}
/
//从新定义hashCode()方法
public int hashCode(){
return this.name.hashCode()+this.age*43;//返回自定义的hashCode值
}
//从新定义equals()方法
public boolean equals(Object obj){
if(!(obj instanceof Student)){//判断,obj向下转型不是Student类的话,抛出异常
throw new ClassCastException("类型不匹配");
}
Student s=(Student)obj;
return this.name.equals(s.name)&&this.age==s.age;
}
//复写Object类中的toString()方法
public String toString(){
return "name:"+this.name+",age:"+this.age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
黑马程序员——HashMap综合练习
最新推荐文章于 2024-01-09 11:30:31 发布