List totaling error using Hash Map

Asked

Viewed 33 times

0

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

What happens is that sometimes the values are ignored generating a difference in the total. And I can’t 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);  
            }
        }

1 answer

0

Good people, I thank everyone who at least looked at the question. But this whole process is ok and calculating correctly. After several tests I discovered that the origin of the problem occurred 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();

Done this all calculations matched with exceptional performance for significant data volumes.

Abs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.