【问题标题】:How result List with 3 columns STREAM结果如何列出 3 列 STREAM
【发布时间】:2016-09-05 16:46:25
【问题描述】:

制作 Java 8 Stream 可让您创建每个产品数量较多的国家/地区列表。

之前(原文):

中国鼠标4

巴西鼠标 3

巴西键盘 3

法国键盘 2

之后(结果):

中国鼠标4

巴西键盘 3

可以做Stream吗?

【问题讨论】:

  • 不确定您需要什么。可能是代码 sn-p 会有所帮助。
  • 除非你提供你的尝试,否则这是不可能的

标签: java arraylist stream java-stream


【解决方案1】:
  public static void main(String[] args) {
      List<Product> productList = new ArrayList<>();
      productList.add(new Product("China","Mouse",4));
      productList.add(new Product("Brazil","Mouse",3));
      productList.add(new Product("Brazil","Keyboard",3));
      productList.add(new Product("France","Keyboard",2));

      productList.stream()
       .collect(Collectors.toMap(Product::getProductName,Product::getQuantity,(q1,q2)->q1<q2?q2:q1));

产品类:

class Product {
    private String productName;
    private String nation;
    private int quantity;

    public Product(String nation, String productName, int quantity) {
        this.productName = productName;
        this.nation = nation;
        this.quantity = quantity;
    }

    public int getQuantity() {
        return quantity;
    }

    public String getProductName() {
        return productName;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 2015-07-01
    • 2016-03-02
    相关资源
    最近更新 更多