0

Hello Im trying to remove an elemento from a HashSet that have an entity created by me that is called "Product" this is the code:

Set listaItemsTrans = this.getTransaction().getTransactionItems();
        HashSet listaIteratorI = new HashSet();

        listaIteratorI.addAll(listaItemsTrans);

        Iterator itemSet = listaIteratorI.iterator();

        while (itemSet.hasNext())
        {
            Product prodBif = (Product) itemSet.next();
            logger.debug("Prod bif = " + prodBif.getIdProduct() + " = " + "Prod search = " + prodSearch.getIdProduct());
            if (prodBif.getIdProduct().compareTo(prodSearch.getIdProduct()) == 0)
            {
                logger.debug("El Producto ha sido encontrado");
                try
                {
                    logger.debug("ProdBif " + prodBif);
                    logger.debug("Lista de productos 3 " + this.getTransaction().getTransactionItems());
                    boolean isRemoved;
                    isRemoved = this.getTransaction().getTransactionItems().remove(prodBif);
                    logger.debug("isRemoved? " + isRemoved);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }

                logger.debug("Lista de productos 4" + this.getTransaction().getTransactionItems());
                prodBif = prodSearch;
                this.getTransaction().getTransactionItems().add(prodBif);
                logger.debug("Lista de productos 5" + this.getTransaction().getTransactionItems());
                isOkey = true;
            }
        }

The product is the same i already check that but im gonna put the product equal:

Product Bif: Product [idProduct=1546405266442777, transaction=Transaction [idTransaction=1546405253162777, transaction=077700514, transactionDate=2019-01-02 10:01:06, session=Session [idSession=1546405226194777, session=00130777000000011111, cashier=com.lds.exito.darchitecture.domainmodel.entities.Cashier@d8241df3, store=Store [idStore=1546405226193777, store=0013, description=EXTITO GRAN VIA M001120], terminal=777, sessionDate=2019-01-02 10:00:26], sequenceNumber=0514, ], pluVariety=000000213690, salePrice=9900, description=NEVERA NO FROST 39, details={CashierID=0000011111, mensaje=Certificado valido, TransactionID=0514, AuthorizationBus=CommunicationHttpBodyAuthBus, ValidationResponseRevertionStatus=com.lds.exito.soa.status.validation.IvaDifResponseStatusValidation, portPrimary=, hostPrimary=http://wsredverde.datagestion.co/ws/?a=2&certificado=3&identificacion=8276946&codigo=13, contentType=application/json, StoreID=0013, PREFIX_AUTH_END_identificacion==8276946, OriginalTerminalID=777, nroIdentificacion=8276946, uri=, AuthorizationClassName=FinalAuthorizationHttp, plu=000000213690, PREFIX_AUTH_END_certificado==3, TerminalID=777, fechaHora=2019-01-02T10:01:06, nroCertificado=3, OriginalTransactionID=0514}, extras={transaction_record=IvaDif/IvaDif-99-129,,, voucher_list=IvaDif/FooterRecarga,}, authorizationType=ParametersConfig [idParameter=2, descriptionGroup=Tipos de Autorizacion, idGroup=TPAUT, descriptionParameter=Autorizacion al Final de la Transaccion, parameterValue=ENDTR], authorizationStatus=ParametersConfig [idParameter=3, descriptionGroup=Estado de la Autorizacion, idGroup=STAUT, descriptionParameter=Pendiente, parameterValue=PNDNG], reverseType=ParametersConfig [idParameter=7, descriptionGroup=Tipos de Reverso, idGroup=TPREV, descriptionParameter=No Aplica, parameterValue=NOAPP], reverseStatus=ParametersConfig [idParameter=8, descriptionGroup=Estado del Reverso, idGroup=STREV, descriptionParameter=No Aplica, parameterValue=NOAPP], anulationType=ParametersConfig [idParameter=11, descriptionGroup=Tipos de Anulacion, idGroup=TPANU, descriptionParameter=Aplica, parameterValue=APPLY], anulationStatus=ParametersConfig [idParameter=13, descriptionGroup=Estado del Anulacion, idGroup=STANU, descriptionParameter=No Aplica, parameterValue=NOAPP], productType=ParametersConfig [idParameter=16, descriptionGroup=Tipos de Producto, idGroup=TPPDT, descriptionParameter=Item, parameterValue=ITEM], quantity=1, ] 

Product in the HashSet: Product [idProduct=1546405266442777, transaction=Transaction [idTransaction=1546405253162777, transaction=077700514, transactionDate=2019-01-02 10:01:06, session=Session [idSession=1546405226194777, session=00130777000000011111, cashier=com.lds.exito.darchitecture.domainmodel.entities.Cashier@d8241df3, store=Store [idStore=1546405226193777, store=0013, description=EXTITO GRAN VIA M001120], terminal=777, sessionDate=2019-01-02 10:00:26], sequenceNumber=0514, ], pluVariety=000000213690, salePrice=9900, description=NEVERA NO FROST 39, details={CashierID=0000011111, mensaje=Certificado valido, TransactionID=0514, AuthorizationBus=CommunicationHttpBodyAuthBus, ValidationResponseRevertionStatus=com.lds.exito.soa.status.validation.IvaDifResponseStatusValidation, portPrimary=, hostPrimary=http://wsredverde.datagestion.co/ws/?a=2&certificado=3&identificacion=8276946&codigo=13, contentType=application/json, StoreID=0013, PREFIX_AUTH_END_identificacion==8276946, OriginalTerminalID=777, nroIdentificacion=8276946, uri=, AuthorizationClassName=FinalAuthorizationHttp, plu=000000213690, PREFIX_AUTH_END_certificado==3, TerminalID=777, fechaHora=2019-01-02T10:01:06, nroCertificado=3, OriginalTransactionID=0514}, extras={transaction_record=IvaDif/IvaDif-99-129,,, voucher_list=IvaDif/FooterRecarga,}, authorizationType=ParametersConfig [idParameter=2, descriptionGroup=Tipos de Autorizacion, idGroup=TPAUT, descriptionParameter=Autorizacion al Final de la Transaccion, parameterValue=ENDTR], authorizationStatus=ParametersConfig [idParameter=3, descriptionGroup=Estado de la Autorizacion, idGroup=STAUT, descriptionParameter=Pendiente, parameterValue=PNDNG], reverseType=ParametersConfig [idParameter=7, descriptionGroup=Tipos de Reverso, idGroup=TPREV, descriptionParameter=No Aplica, parameterValue=NOAPP], reverseStatus=ParametersConfig [idParameter=8, descriptionGroup=Estado del Reverso, idGroup=STREV, descriptionParameter=No Aplica, parameterValue=NOAPP], anulationType=ParametersConfig [idParameter=11, descriptionGroup=Tipos de Anulacion, idGroup=TPANU, descriptionParameter=Aplica, parameterValue=APPLY], anulationStatus=ParametersConfig [idParameter=13, descriptionGroup=Estado del Anulacion, idGroup=STANU, descriptionParameter=No Aplica, parameterValue=NOAPP], productType=ParametersConfig [idParameter=16, descriptionGroup=Tipos de Producto, idGroup=TPPDT, descriptionParameter=Item, parameterValue=ITEM], quantity=1, ]

And i dont know why that is happening, something that i miss?

Note 1: when i try to do the same thing with another product that code works

Note 2: Im using java 1.4 by restrictions in the hardware

  • 1
    Can you show the code of the `equals` method in `Product` class. – Amit Bera Jan 02 '19 at 15:34
  • Wow, sorry to hear about the java 1.4 restriction, almost wanted to throw in my obligatory "don't use rawtypes"-comment, but alas - a time travel back to the non-generic past – Hulk Jan 02 '19 at 15:36
  • 1
    Does `equals` method in `Product` work correctly? Does `prodBif.equals(prodBif)` return `true`? – Amongalen Jan 02 '19 at 15:37
  • But i not using method equals in product so i dont know why is that important – Juan Jose Rojas Velez Jan 02 '19 at 15:40
  • `Set` uses it internally when you call `remove` – Hulk Jan 02 '19 at 15:42
  • As a side note, it is a bit confusing to call an iterator `itemSet` and the set it iterates `listaItemsTrans`... and another set `listaIteratorI` – Hulk Jan 02 '19 at 15:44
  • Ok, im gonna try the equals in the entyty Product work correctly – Juan Jose Rojas Velez Jan 02 '19 at 15:45
  • also check the `hashcode()` - it needs to be consistent with `equals` for a `HashSet` to work correctly. – Hulk Jan 02 '19 at 15:47
  • I need do that because im going to do updates from the hashset so if i try to do that in the same set that im itering java put an exception by ConcurredHashModifications @Hulk – Juan Jose Rojas Velez Jan 02 '19 at 15:47
  • @JuanJoseRojasVelez I was just referring to the names - name your sets something with `xyzSet`, and you iterators `xyzIterator`, and don't name a Set `list` - that is just confusing – Hulk Jan 02 '19 at 15:53
  • Can you please post the `equals` and `hashCode` implementations? `Hash*` classes heavily relies on `hashCode` implementation to properly hash the values. – sweet suman Jan 02 '19 at 16:03
  • Also - have you checked that `this.getTransaction().getTransactionItems()` is really just a getter and does not return a snapshot or something? – Hulk Jan 02 '19 at 16:03
  • I found the problem, is the hashcode but i dont understand why, the 2 objects are the same, so in theory the hashcode must be the same – Juan Jose Rojas Velez Jan 02 '19 at 16:29
  • Unless you show us the relevant `hashcode`/`equals` implementations, we cannot provide any further help. If your problem is resolved, you could either just delete your question (as in its current form, it is not likely to be helpful for future readers), or write an answer explaining what the problem was, and mark is as accepted. – Hulk Jan 03 '19 at 06:08

0 Answers0