2021-11-11

用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);
    }

上一篇:源壁纸微信小程序源码,小米壁纸自动更新采集


下一篇:轻言壁纸微信小程序源码