csv 用Java流过滤器可视化错误数据

ssgvzors  于 2023-02-06  发布在  Java
关注(0)|答案(1)|浏览(157)

我有一个证券交易所的csv文件,我从其中提取数据,在某些条件下读取并打印它。我必须显示它们的条件之一是显示每一股的最低价值。我的代码只显示三只股票中每一只的第一条记录,而不显示这些股票的最低价值。我在使用过滤器进行过滤时哪里出错了?
我的代码

private static void smallestPriceByStock(List<String> stocks) throws ParseException {
        List<StockPrice> stockPrices = new ArrayList<>();
        HashSet<String> stock = new HashSet<>();
        SimpleDateFormat sdfOriginal = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdfNovaData = new SimpleDateFormat("dd/MM/yyyy");

        try {
            for (String s : stocks) {
                String[] array = a.split(",");
                String name = array[0];
                String date = array[1];
                Date dataOriginal = sdfOriginal.parse(date);
                float fechamento = Float.parseFloat(array[2]);
                long volume = Long.parseLong(array[3]);
                StockPrice stockPrice = new StockPrice(nome, dataOriginal, price, volume);
                stockPrices.add(stockPrice);
                Stock st = new Stock(name);
                st.addStockPrice(stockPrice);
                stock.add(st.getNome());

            }
            System.out.println("Menores fechamentos");
            List<Cotacao> cotas = new ArrayList<>();
            double smallestValue = 0;
            for(String a : acao) {
                for (Cotacao c : cotacoes) {
                    if (c.getNome().contains(a)) {
                        smallestValue = cotacoes.stream()
                                .filter(x -> x.getNome() == a)
                                .mapToDouble(x -> x.getValue()).summaryStatistics().getMax();
                            System.out.println("Stock: " + c.getNome() + ", " + "Price: " + smallestValue + ", " + "Date: " + sdfNovaData.format(c.getData()));
                            break;
                        }
                    }
                }

            for(Cotacao cotacao : cotas){
                System.out.println(cotacao.getNome());
            }
            System.out.println();
        } catch (UnsupportedOperationException | ParseException e) {
            throw new UnsupportedOperationException("calculaMenorFechamentoPorAcao não implementado", e);
        }
    }

csv文件的一个小样本

Acao,Data,Fechamento,Volume
OGXP3,2013-01-01,4.38,0
OGXP3,2013-01-02,4.76,45904000
OGXP3,2013-01-03,4.90,38143400
PETR4,2013-01-02,19.69,30182600
PETR4,2013-01-03,20.40,30552600
PETR4,2013-01-04,20.43,36141000
PETR4,2013-01-07,20.08,28069600
VALE5,2013-01-01,40.87,0
VALE5,2013-01-02,42.60,18515700
VALE5,2013-01-03,42.09,15001800
VALE5,2013-01-04,41.36,26351900
ffscu2ro

ffscu2ro1#

你把它弄得太复杂了,找到最小值的常用伪代码是:

set lowestValue to null
for each value
   if lowestValue == null || value < lowestValue
       lowestValue = value

由于要查找多个值,每个值都由股票名称标识,因此将lowest_value定义为Map<String, BigDecimal>,其中key是股票标识符。处理完输入数据中的每一行后,Map将包含每个股票的最低值。无需嵌套循环,因为所有操作都在单次传递中完成。填充Map后,可以在Map数据上以单独的单个循环进行打印。

相关问题