比较两个对象 取最大值 并返回对象
Posted G_whang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了比较两个对象 取最大值 并返回对象相关的知识,希望对你有一定的参考价值。
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+ "";
以上是关于比较两个对象 取最大值 并返回对象的主要内容,如果未能解决你的问题,请参考以下文章