Home » Java » java – Remove Element in ArrayList in loop-Exceptionshub

java – Remove Element in ArrayList in loop-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

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.

How to&Answers:

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]

Answer:

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(); 
}