Error in totalizing a list using Hash Map

0

I sort a list in a certain order, then create a Map to total this list by grouping by a key.

What happens is that sometimes the values are ignored generating difference in total. And I can not see where this might be occurring see the code:

// Classifica

listaLancamentos.sort(new OrdenaVolumeProdutoPorNome().thenComparing(new       OrdenaVolumeProdutoVariedadePorNome()).thenComparing(new OrdenaVolumeProdutoPorUf())
                .thenComparing(new OrdenaVolumeProdutoPorMunicipio()));

        // Totaliza volumes

        Map<String, DtoVolumeProduto> map = new HashMap<String, DtoVolumeProduto>();

        for (DtoVolumeProduto dvl : listaLancamentos) {
            String key = dvl.getNmProduto()+" "+dvl.getNmProdutoVariedade()+" "+dvl.getSgUf()+" "+dvl.getNmMunicipio();
            if (!map.containsKey(key)) {

                map.put(dvl.getNmProduto()+" "+dvl.getNmProdutoVariedade()+" "+dvl.getSgUf()+" "+dvl.getNmMunicipio(), dvl);
                // logger.info(key+" - "+dvl.getVolumeKg()+" "+dvl.getPrecoMedioKg()+"
                // "+dvl.getVrTotal());

            } else {
                DtoVolumeProduto dto = map.get(key);

                // logger.info(key+" "+dvl.getVolumeKg());

                BigDecimal volumeKg = dto.getVolumeKg().setScale(3, RoundingMode.HALF_UP).add(dvl.getVolumeKg().setScale(3, RoundingMode.HALF_UP));
                dto.setVolumeKg(volumeKg);  
            }
        }
    
asked by anonymous 25.09.2018 / 14:11

1 answer

0

Good people, thank you all who at least looked at the question. But this whole process is okay and calculating correctly. After several tests I discovered that the origin of the problem was in the source list that came with duplications, just put

Before:

return criteria.list ();

Now:

return criteria.setResultTransformer (Criteria.DISTINCT_ROOT_ENTITY) .addOrder (Order.desc ("id")) list ();

As a result all calculations have achieved exceptional performance for significant data volumes.

Abs

    
26.09.2018 / 19:03