如何从ArrayList获取最高值并显示信息

时间:2015-09-03 21:05:32

标签: java

我需要从ArrayList中找到最高价值(销售额),并显示每个季度销售额最高的商店。

        BufferedReader inputFile = null;
        PrintWriter outputFile = null;

        BookStore bs;
        ArrayList <BookStore> data = new ArrayList();

        try
        {
            inputFile = new BufferedReader (new FileReader ("bookstoresales.txt"));
            outputFile = new PrintWriter (new FileWriter ("bookstoreoutput.txt"));

            String indata = null;

            while ((indata = inputFile.readLine()) != null)
            {
                System.out.println (indata);
                StringTokenizer st = new StringTokenizer (indata, ",");

                String storeBranch = st.nextToken();
                String storeAddress = st.nextToken();
                double jan = Double.parseDouble (st.nextToken());
                double feb = Double.parseDouble (st.nextToken());
                double mac = Double.parseDouble (st.nextToken());
                double apr = Double.parseDouble (st.nextToken());
                double may = Double.parseDouble (st.nextToken());
                double jun = Double.parseDouble (st.nextToken());
                double jul = Double.parseDouble (st.nextToken());
                double aug = Double.parseDouble (st.nextToken());
                double sep = Double.parseDouble (st.nextToken());
                double oct = Double.parseDouble (st.nextToken());
                double nov = Double.parseDouble (st.nextToken());
                double dec = Double.parseDouble (st.nextToken());

                bs = new BookStore (storeBranch, storeAddress, jan, feb, mac, apr, may, jun, jul, aug, sep, oct, nov, dec);

                data.add(bs);
            }

            outputFile.println ("\n\n=================================== The Highest Bookstore Sales Per Quarter  ===================================");


            for (int i=0; i<data.size(); i++)
            {
                //This is where I got stucked.
            }

            outputFile.println ("\nQuarter 1 : The Highest sales is Bookstores : " + //getbranch name from the highest sales  + " with value of + //the highest value");


            inputFile.close();
            outputFile.close();
        }

        catch (FileNotFoundException fnf)
        {
            System.out.println ("File not found!");
        }

        catch (IOException ioe)
        {
            System.out.println (ioe.getMessage());
        }
    }
}

现在我需要获取并显示商店名称以及最高的销售价值。对于ArrayList,我很困惑。

3 个答案:

答案 0 :(得分:2)

创建一个名为max的变量。在循环中,如果列表中的项目高于max,请将max设置为该项目。

BookStore max = data.get(0);
for (BookStore bs : data) {
    if (bs.sales > max.sales)  {
        max = bs;
    }
}

答案 1 :(得分:1)

使用Java 8:

BookStore max = 
    data.stream()
        .max(Comparator.comparingDouble(bs -> bs.getJan() + ... + bs.getDec()))
        .get();

答案 2 :(得分:0)

另一种方法是使用Collections.max()

Collections.max(data, new Comparator<BookStore>() {
    @Override
    public int compare(BookStore a, BookSore b) {
        return a.sales - b.sales;
    }
});

如果BookStoreComparable,则只需:

Collections.max(data);
相关问题