import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
* 比较两个对象 取最大值 并返回对象
*/
public class Test009 {
public static void main(String[] args) {
List<Goods> goodsList = getGoodsList();
Goods goods = goodsList.stream().max((o1, o2) -> {
BigDecimal bigDecimal1 = o1.getAmount();
BigDecimal bigDecimal2 = o2.getAmount();
return bigDecimal1.compareTo(bigDecimal2);
}).get();
System.out.println(goods.toString());
}
public static List<Goods> getGoodsList(){
List<Goods> goods = new ArrayList<>();
goods.add(new Goods("瓜子",new BigDecimal("20"),"5"));
goods.add(new Goods("辣条",new BigDecimal("13"),"2"));
goods.add(new Goods("肥仔快乐水",new BigDecimal("30"),"4"));
return goods;
}
public static class Goods{
/**
* 商品名称
*/
private String name;
/**
* 商品价格
*/
private BigDecimal amount;
/**
* 商品数量
*/
private String num;
public Goods() {
}
public Goods(String name, BigDecimal amount, String num) {
this.name = name;
this.amount = amount;
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
@Override
public String toString() {
return "Goods{" + "name=" + name +" "+"amount=" +amount+" "+"num=" +num+ "}";
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88