尝试按升序反转我的流 - Java 8

Trying to reverse my stream in ascending order - Java 8

这段代码背后的想法是,它从三个 .csv 文件中读取数据,这些文件带有按年份组织的键,并按年份检索数据的总和,以及最小和最大数量每年。

问题是,打印时 returns 年度结果按降序排列。

我需要帮助找出一种方法来反转流,以便每个文件的年度数据按升序排列。

public class SalesGenerator {
    
    //This class will print out the Maximum and minimum Sales data from the .getSales() in TeslaImport.
    public static void teslaReport(
        Set<Entry<String, List<TeslaImport>>> entrySet, 
        List<TeslaImport> teslaModel, String modelName) 
    throws IOException {        
        //This comparator will sort the sales array values.
        Comparator<TeslaImport> comparativeOperator = Comparator.comparing(tesla -> tesla.getSales());

        entrySet.stream().forEach(entry -> System.out.println(
            "Month: " + entry.getKey() + " -> " 
            + entry.getValue().stream()
                .mapToInt(tesla -> tesla.getSales().intValue())
                .sum()
        ));
        
        TeslaImport maximumSales = teslaModel.stream()
            .max(comparativeOperator)
            .orElseThrow(NoSuchElementException::new);
        TeslaImport minimumSales = teslaModel.stream()
            .min(comparativeOperator)
            .orElseThrow(NoSuchElementException::new);
            
        //Print out the maximum and minimum results from TeslaReport.
        System.out.println(modelName + " Yearly Sales Report");
        System.out.println("The BEST month for " + modelName + " was: " + maximumSales.getDate());
        System.out.println("The WORST month for " + modelName + " was: " + minimumSales.getDate());                                                                                 
    } 
    
    public static void main(String[] args) throws IOException {
        
        /* To produce the sales values from TeslaData, we need to instantiate the TeslaData variable to import
        all the EntrySet models */
        TeslaData analysedData = new TeslaData();
        
        /*The FileReader from our FileGenerator class needs to be instantiated to model3. We can use to when
        when reporting our data in TeslaReport */
        List<TeslaImport> model3 = FileGenerator.teslaFileRead("model3.csv");
        Set<Entry<String, List<TeslaImport>>> entrySetModel3 = analysedData.entryByYear(model3);
        teslaReport(entrySetModel3, model3, "Model 3");
        
        /*The FileReader from our FileGenerator class needs to be instantiated to modelS. We can use to when
        when reporting our data in TeslaReport */
        List<TeslaImport> modelS = FileGenerator.teslaFileRead("modelS.csv");
        Set<Entry<String, List<TeslaImport>>> entrySetModelS = analysedData.entryByYear(modelS);
        teslaReport(entrySetModelS, modelS, "Model S");
        
        /*The FileReader from our FileGenerator class needs to be instantiated to modelX. We can use to when
        when reporting our data in TeslaReport */
        List<TeslaImport> modelX = FileGenerator.teslaFileRead("modelX.csv");   
        Set<Entry<String, List<TeslaImport>>> entrySetModelX = analysedData.entryByYear(modelX);        
        teslaReport(entrySetModelX, modelX, "Model X");
    }
}

注意: TeslaImport 是 class 我放置 JavaBean 的 (get/set, toString, date, 和 sales)

由于在TeslaModelclass中为方法entryByYear检索到的条目集打印了报告数据,因此很可能该方法returns条目设置为按降序排序的树图。因此,要更改顺序,流应该 re-sorted by key:

entrySet.stream()
    .sorted(Map.Entry.comparingByKey())
    .forEach(entry -> System.out.println(
            "Month: " + entry.getKey() + " -> " 
            + entry.getValue().stream()
                .mapToInt(tesla -> tesla.getSales().intValue())
                .sum()
        ));