.---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
package heimaLog;
import java.util.*;
import java.util.Map.Entry;
/*
* 每一个学生都有对应的归属地
* 学生Stuedent,地址String
* 学生属性:姓名,年龄
* 注意:姓名和年龄相同的视为同一个学生
* 保证学生的唯一性
*
* */
class SStudent<String,Integer>{
private String name;
private int age;
private String addrees;
SStudent(){}
SStudent(String name,int age){
this.name = name;
this.age = age;
}
public int hashCode(){
return name.hashCode()+age+1;
}
public boolean equals(Object obj){
SStudent s = (SStudent)obj;
return name.equals(s.name)&&age==s.age;
}
public String getname(){
return name;
}
public int getage(){
return age;
}
}
public class MapTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<SStudent<String,Integer>,String> map = new HashMap<SStudent<String,Integer>,String>();
map.put(new SStudent("zhangsan",22),"beijing");
map.put(new SStudent("lisi",23),"tianjin");
map.put(new SStudent("wangwu",28),"shanghai");
map.put(new SStudent("zhaoliu",14),"chengdu");
map.put(new SStudent("huangermazi",22),"jinan");
map.put(new SStudent("huangermazi",22),"jinan");
Set<Map.Entry<SStudent<String,Integer>, String>> set = map.entrySet();
Iterator<Map.Entry<SStudent<String,Integer>, String>> it = set.iterator();
while(it.hasNext()){
Map.Entry<SStudent<String,Integer>, String> me =it.next();
sop(me.getKey().getname()+"----"+me.getKey().getage()+"-------"+me.getValue());
}
}
public static void sop(Object obj){
System.out.println(obj);
}
}
----------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------