用于映射

时间:2015-12-07 01:13:34

标签: java java-stream

我有这堂课:

package ProcessInvoices;

public class ProcessInvoices {

private final int partNumber;
private final String partDescription;
private int quantity;
private double price;

// constructor
public ProcessInvoices(int partNumber, String partDescription, int quantity, double price) {
    if (quantity < 0) // validate quantity
    {
        throw new IllegalArgumentException("Quantity must be >= 0");
    }

    if (price < 0.0) // validate price
    {
        throw new IllegalArgumentException(
                "Price per item must be >= 0");
    }

    this.partNumber = partNumber;
    this.partDescription = partDescription;
    this.quantity = quantity;
    this.price = price;
} // end constructor

// get part number
public int getPartNumber() {
    return partNumber; // should validate
}

// get description
public String getPartDescription() {
    return partDescription;
}

// set quantity
public void setQuantity(int quantity) {
    if (quantity < 0) // validate quantity
    {
        throw new IllegalArgumentException("Quantity must be >= 0");
    }

    this.quantity = quantity;
}

// get quantity
public int getQuantity() {
    return quantity;
}

// set price per item
public void setPrice(double price) {
    if (price < 0.0) // validate price
    {
        throw new IllegalArgumentException(
                "Price per item must be >= 0");
    }

    this.price = price;
}

// get price per item
public double getPrice() {
    return price;
}

// return String representation of ProcessInvoices object
@Override
public String toString() {
    return String.format(
            "Part #: %-2d  Description: %-15s  Quantity: %-4d  Price: $%,6.2f",
            getPartNumber(), getPartDescription(),
            getQuantity(), getPrice());
}

} 

这是司机:

package ProcessInvoices;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ProcessInvoicesDriver {

    public static void main(String[] args) {
    ProcessInvoices[] Invoice = {
        new ProcessInvoices(83, "Electric sander", 7, 57.98),
        new ProcessInvoices(24, "Power saw", 18, 99.99),
        new ProcessInvoices(7, "Sledge hammer", 11, 21.50),
        new ProcessInvoices(77, "Hammer", 76, 11.99),
        new ProcessInvoices(39, "Lawn mower", 3, 79.50),
        new ProcessInvoices(68, "Screwdriver", 106, 6.99),
        new ProcessInvoices(56, "Jig saw", 21, 11.00),
        new ProcessInvoices(3, "Wrench", 34, 7.50)};

    List<ProcessInvoices> list = Arrays.asList(Invoice);


    ////////////////////////////////////////Invoices sorted by part description
    Function<ProcessInvoices, String> desc = ProcessInvoices::getPartDescription;
    Comparator<ProcessInvoices> byPartDesc = Comparator.comparing(desc);

    System.out.printf("%nInvoices sorted by part description:%n");
    list.stream().sorted(byPartDesc).forEach(System.out::println);

    ///////////////////////////Invoices sorted by price
    Function<ProcessInvoices, Double> price = ProcessInvoices::getPrice;
    Comparator<ProcessInvoices> byPrice = Comparator.comparing(price);

    System.out.printf("%nInvoices sorted by price:%n");
    list.stream().sorted(byPrice).forEach(System.out::println);
    System.out.printf("\n");

    ///////////////// This Part Below is what i need help with ///// 
    Function<ProcessInvoices, Integer> quantity = ProcessInvoices::getQuantity;

    Comparator<ProcessInvoices> byquantity = Comparator.comparing(quantity);

    // display only first and last names
    System.out.printf("%nInvoices mapped to description and quantity:%n");
    list.stream()
            .sorted(byquantity)
            .map(ProcessInvoices::getPartDescription)
            .forEach(System.out::println);

}

}

在驱动程序上,它说“这部分我需要帮助”我该怎么做:

  

使用lambda和流将每个Invoice映射到其PartDescription和Quantity,按Quantity排序结果,然后显示结果。

并输出如下:

Invoices mapped to description and quantity:
Description: Lawn mower       Quantity: 3
Description: Electric sander  Quantity: 7
Description: Sledge hammer    Quantity: 11
Description: Power saw        Quantity: 18
Description: Jig saw          Quantity: 21
Description: Wrench           Quantity: 34
Description: Hammer           Quantity: 76
Description: Screwdriver      Quantity: 106

我已经做了很多事情,我无法让它发挥作用:我在上面的脚本中所拥有的是我尝试过的最后一件事。

2 个答案:

答案 0 :(得分:0)

这有点像家庭作业问题,因此请确保您在此处寻求帮助,而不是违反学校的荣誉准则。

就答案而言,你可以尝试这样的事情:

list.stream()
    .collect(Collectors.toMap(desc,quantity,(q1,q2) -> q1 + q2)))
    .entrySet()
    .stream()
    .sorted(Map.Entry.comparingByValue())
    .forEach(e -> System.out.println(String.format("Description: %-15s  Quantity: %-4d",e.getKey(),e.getValue())));

collect语句将流转换为描述,数量的映射,然后对生成的条目集进行排序和打印。 (q1,q2) - >如果列表包含重复描述,则q1 + q2表达式是用于处理合并的逻辑。在这种情况下,我假设组合结果应该是两个量的总和。如果输入列表包含重复项是错误的,则可以使用Collections.toMap()的两个参数版本,如果遇到重复键,则会抛出异常。

有关Collections.toMap(...)的更多信息,您可以阅读Java 8 javadocs here

答案 1 :(得分:0)

你也可以在这里尝试类似这个例子的东西,甚至可以使用你现在拥有的东西:

System.out.println("Invoices sorted by part description:"); 

Arrays.stream(invoices)          
.sorted(Comparator.comparing(Invoice::getPartDescription))          
.forEach(System.out::println);