0

I need help. I tried to remove an element from an ArrayList. I have two lists. One list from a file, the second list from the database.

I need to find the same elements to later remove them from the original list, and thus have a list with differences.

List<BinInternacionalMarcaDEL> listDiff = new ArrayList<BinInternacionalMarcaDEL>();

ListOriginal= binInternacionalRepositoryDEL.findAllByBin();

public  List<BinInternacionalMarcaDEL> Differences(List<BinInternacionalMarcaDEL> listA,
                                                   List<BinInternacionalMarcaDEL> listB) {
    try {
        for(BinInternacionalMarcaDEL elementA: listaA){
            for(BinInternacionalMarcaDEL elementB: listaB) {
                if(elementA.getNumRangoini().compareTo(elementB.getNumRangoini()) == 0 ){
                    listDiff.add(elementA);
                }
            }
        }
        ListOriginal.removeAll(listDiff);
    } catch (Exception e) {
        LOGGER.error(e.toString());
    }

but this doesn't work.

10
  • What is the type of NumRangoini ? Commented Feb 18, 2020 at 2:34
  • Does this SO question help? How can I return the difference between two lists? Commented Feb 18, 2020 at 2:37
  • NumRangoini is a BigInteger, the class BinInternacionalMarcaDEL have two bigInteger:NumRangoini and NumRangoFin,the "IF" is working, the problem is remove Commented Feb 18, 2020 at 2:37
  • 1
    Could you add the declaration of listDiff and ListOriginal in your question Commented Feb 18, 2020 at 2:46
  • Are you getting unmodifiable exception ? Commented Feb 18, 2020 at 2:47

2 Answers 2

1

just you can do one thing

 listA.retainAll(listB);

here now listA contains only similar elements in both ListA and ListB.

Example:

List<String> listA =  new ArrayList<>(Arrays.asList("12","13","15","2","5")) ;   
List<String> listB =  new ArrayList<> (Arrays.asList("2","12","48","49","55"));
listA .retainAll(listB );
System.out.println(listA ); //[12, 2]
Sign up to request clarification or add additional context in comments.

1 Comment

The class BinInternacionalMarcaDEL should also implement equals and hashCode. Your example works, because String does that.
1

Java list remove and contains methods are implemented using equality of objects. This logic is implemented in hashCode and equal method in your class and all classes in Java inherit this attribute from the Object class.(to be honest ArrayList doesn't use hashCode in implementing remove and contains metheds but in java contract you should implement these two methods together). here when you are adding element to listDiff you are defining your own equality which is based on equality of attribute numRangoini(using compateTo method) and when you request the list class to remove them from list(with removeAll method). removeAll remove them based on the equality of that two object. Since you haven't defined this logic in your own class. this behaviour is inherited from object class which is based on strict equality. by default two object are equal if they reference the same object.

Solution: define the logic for equality in your own class in equal method. This method should return true if two object has the same attribute value numRangoini. don't forget to define hashCode as well. and here is the rule if two objects are equal they should return the same hashCode.

here is a simple implementation

@Override
public boolean equals(Object obj) { 
    if(this == obj) 
        return true;  
    if(obj == null || obj.getClass()!= this.getClass()) 
        return false; 
    BinInternacionalMarcaDEL  binInternacionalMarcaDEL  = (BinInternacionalMarcaDEL)obj;
    return (national.getNumRangoini().compareTo(this.getNumRangoini()) == 0); 
} 



@Override
public int hashCode() { 
   return this.getNumRangoini().intValue(); 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.