这只是一种情况吧哥们。假如说我有一个学生类,要求你用集合并使用泛型修饰,将学生这个类传进去,那你的第二种情况还效率高吗?
两种不一样,一般在我下面的这种情况下用的是高效率的
public static void main(String[] args) {
Map<Mouse,Cat> map=new TreeMap<Mouse,Cat>();
Cat anim=new Cat("234", 23);
Cat cat=new Cat("322", 12);
Mouse mou=new Mouse("ww", 43);
Mouse mou1=new Mouse("fw", 23);
map.put(mou1, cat);
map.put(mou, anim);
Set<Entry<Mouse, Cat>> kets=map.entrySet();
Iterator<Entry<Mouse, Cat>> it=kets.iterator();
while(it.hasNext()){
Entry<Mouse, Cat> ca=it.next();
Set<Mouse> mo=(Set<Mouse>) ca.getKey();
Iterator<Mouse> s= mo.iterator();
while(s.hasNext()){
Mouse mouse=s.next();
System.out.println(mouse.getAge()+","+mouse.getName());
Set<Cat> e=(Set<Cat>) ca.getKey();
Iterator<Cat> ss= e.iterator();
while(ss.hasNext()){
Cat vt=map.get(mouse);
System.out.println(vt.getAge()+","+vt.getName());
}
}
}
}
}
class Mouse{
private String name;
private int age;
Mouse(String name,int age){
this.name=name; this.age=age;
}
public void getSong(String belongid){
System.out.println("被猫咪咬过之后老鼠的声音是"+belongid);
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return name+","+age;
}
}
class Cat {
String name;
int age;
Cat(String name,int age)
{
this.name=name;
this.age=age;
}
public void Animal(String name,int age){
this.name=name;
this.age=age;
}
public void getSong(String name,int age){
System.out.println(name+"的年龄"+age+",他的叫声是这个声音还行吧!");
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return name+","+age;
}
}
如果你可以理解,就改改这个代码,他就是你没有考虑到的情况 |