先来一个故事背景把,咱们现在在给一位农民伯伯设计一个苹果库存管理系统。他现在有这样的一个需求:想要对苹果库存里面的所有苹果,按照苹果的重量weight排序。这里咱们简单的使用List集合作为我们的仓库,快来看看如何一步一步的实现的更加简洁吧。
先来一个Apple类
public class Apple {
private String color;
private Double weight;
public Apple(String color, Double weight) {
this.color = color;
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Apple{" +
"color='" + color + '\'' +
", weight=" + weight +
'}'