用Stream流取出list集合中,某个属性最大的那个
创建一个类
public class ProductVO implements Serializable{
private String Name;
private String brand;
private double price;
public double getPrice() {
return price;
}
}
创建三个对象放入集合中,
@SpringBootTest
class DemoApplicationTests {
@Test
void contextLoads() {
ProductVO xiaomi = new ProductVO();
xiaomi.setName("小米8");
xiaomi.setBrand("小米");
xiaomi.setPrice(3000);
ProductVO huawei = new ProductVO();
huawei.setName("华为8");
huawei.setBrand("华为");
huawei.setPrice(4000);
ProductVO zhongxing = new ProductVO();
zhongxing.setName("中兴8");
zhongxing.setBrand("中兴");
zhongxing.setPrice(5000);
ArrayList<ProductVO> productVOS = new ArrayList<>();
productVOS.add(xiaomi);
productVOS.add(huawei);
productVOS.add(zhongxing);
//取出价格最低的手机
ProductVO productVO = productVOS.stream().min(Comparator.comparing(ProductVO::getPrice)).get();
System.out.println(productVO);
}
版权声明:本文为weixin_43735471原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。