请教Java TreeMap问题....谢谢!
调用Comparator接口,根据几所高校的面积排序....程序已经没有报错了,运行不了!!!
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.TreeMap;
public class CompDemo {
/**
* @param <T>
* @param args
*/
public static <T> void main(String[] args) {
// TODO Auto-generated method stub
TreeMap<String,Integer> tp=new TreeMap<String,Integer>();
tp.put("学校1", 5500);
tp.put("学校2",4000);
tp.put("学校3",3500);
tp.put("学校4",7000);
tp.put("学校5",2000);
PopComp bvc =new PopComp(); //以下几行有问题
List<String> newList=new ArrayList<String>(tp.keySet());
Collections.sort(newList, bvc);
for(Object o:newList)
{
University u=(University)o;
u.rank(); //调用输出信息的方法
} }
}
class University
{
String uniName;
int area;
public String getUniName() {
return uniName;
}
public void printInfo() {
// TODO Auto-generated method stub
}
public void setUniName(String uniName) {
this.uniName = uniName;
}
public int getarea() {
return area;
}
public void setarea(int area) {
this.area = area;
}
public University() {
// TODO Auto-generated constructor stub
}
public void rank()
{
System.out.println (uniName+"\t"+area);
}
}
class PopComp implements Comparator<Object>
{
public int compare(Object o1, Object o2) {
University u1=(University)o1; //这里也有问题
University u2=(University)o2;
return u2.area-u1.area;
}
}
------解决方案--------------------
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.TreeMap;
public class CompDemo {
/**
* @param <T>
* @param args
*/
public static <T> void main(String[] args) {
// TODO Auto-generated method stub
TreeMap<String, University> tp = new TreeMap<String, University>();
University u1=new University();
tp.put("学校1", new University("学校1",5500));
tp.put("学校2", new University("学校2",4000));
tp.put("学校3", new University("学校3",3500));
tp.put("学校4", new University("学校4",7000));
tp.put("学校5", new University("学校5",2000));
PopComp bvc = new PopComp(); // 以下几行有问题
List<University> newList = new ArrayList<University>(tp.values());
Collections.sort(newList, bvc);
for (Object o : newList) {
University u = (University) o;
u.rank(); // 调用输出信息的方法
}
}
}
class University {
String uniName;
int area;
public String getUniName() {
return uniName;
}
public void printInfo() {
// TODO Auto-generated method stub
}
public void setUniName(String uniName) {
this.uniName = uniName;
}
public int getarea() {
return area;
}
public void setarea(int area) {
this.area = area;
}
public University() {
// TODO Auto-generated constructor stub
}
public University(String uniName,int area){
this.uniName=uniName;
this.area=area;
}
public void rank() {
System.out.println(uniName + "\t" + area);
}
}
class PopComp implements Comparator<Object> {
public int compare(Object o1, Object o2) {