日期:2014-05-20 浏览次数:20619 次
import java.util.*;
public class PropTest{
public static void main(String[] args){
Product p1 = new Product("西瓜",1.0);
Product p2 = new Product("桃子",6.0);
Product p3 = new Product("苹果",4.0);
Product p4 = new Product("草莓",2.0);
Product p5 = new Product("樱桃",12.0);
SortedMap pr = new TreeMap();
pr.put(p1,"10kg");
pr.put(p2,"20kg");
pr.put(p3,"11kg");
pr.put(p4,"13kg");
pr.put(p5,"14kg");
System.out.println(pr.size());
Set s = pr.keySet();
Iterator it = s.iterator();
while(it.hasNext()){
Object o1 = it.next();
Object v = pr.get(o1);
System.out.println("key:"+o1+",value:"+v);
}
}
}
class Product implements Comparable{
String name;
double price;
Product(String name,double price){
this.name = name;
this.price = price;
}
public String toString(){
return "[name:"+name+" price:"+price+"]";
}
public int compareTo(Object o){
Product o1 = (Product)o;
double price1 = this.price;
double price2 = o1.price;
if(price1>price2){
return 1;
}else if(price1>price2){
return -1;
}else return 0;
}
}