使用Java 8 Stream可以创建每个产品的更高数量的国家列表。
之前(原创):
China Mouse 4
巴西鼠标3
巴西键盘3
法国键盘2
之后(结果):
China Mouse 4
巴西键盘3
可以制作Stream?
答案 0 :(得分:0)
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));
具有Product类:
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;
}
}