简体   繁体   中英

How do I find the min, max and avg values from a (double) ArrayList to open a GUI (graph)?

For Full Context: This is code within a JFrame class/JButton used to open up data and to present a content pane that allows its user to open up a graph once a certain button within it is clicked (so the code is within an ActionListener). All code related to opening the graph (which is also on a seperate class) itself works fine.

The content pane also allows its user to open up a CSV file with the same data by clicking on a radio button that says 'CSV' and clicking on a button labelled as 'Load', another button that says 'Show' allows the data within those files to be posted on an (AWT) text area. This has already been done without any issues whatsoever.

Since there are thousands of rows in each column of the files (so a lot of data basically), I need to find the minimum, maximum, and averages of a couple of the columns in them. The 'mm' and 'temperature' columns are two contents of a datalist in a seperate class in the same package.

JButton btnMinMaxAvg = new JButton("Min/Max/Avg");

   btnMinMaxAvg.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e) {


        ArrayList<Double> mm = new ArrayList<Double>();
        ArrayList<Double> temperature = new ArrayList<Double>();

        for(data f: dataList){
            mm.add(f.getMm());
            temperature.add(f.getTemperature());

            LineChartData l = new LineChartData("Plot Data");
            l.setMm(mm);
                l.setTemperature(Temperature);

            l.plot();

               }

    });
        btnMinMaxAvg.setBounds(315, 65, 89, 23);
        contentPane.add(btnMinMaxAvg);


}

The above code simply opens a graph containing the values of these columns, but I've been having trouble finding the minimum, maximum, and average values. Even information regarding one of these would be extremely helpful. Thank you.

With Java 8, three lines:

    DoubleSummaryStatistics summaryStatistics = temperature.stream().mapToDouble(Double::doubleValue).summaryStatistics();
    double minTemp = summaryStatistics.getMin();
    double maxTemp = summaryStatistics.getMax();
    double average = summaryStatistics.getAverage();

You need to call mapToDouble(Double::doubleValue) because you have a List<Double> but you don't need it if you create the Stream directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM