khalid alhussein
khalid alhussein

Reputation: 29

how to find elements of my custom array list that are not present in another array list

ArrayList<ParkingList> Parkings = new ArrayList<ParkingList>();
ArrayList<ParkingList> ParkingsDB = new ArrayList<ParkingList>();

for example, Parkings may contain (a,b,c,d) objects and ParkingsDB may contain (a,b)

how can i find c,d

i tried using this method but it didint work,

ArrayList<ParkingList> temp = new ArrayList<ParkingList>(Parkings);
temp.removeAll(ParkingsDB);

my class definition:

public class ParkingList {
     Rectangle R;
     String Name;
     int level;
     String BuildingName;

     public ParkingList(String BuildingName,String Name, Rectangle R, int level) {
         this.R=R;
         this.Name=Name;
         this.level=level;
         this.BuildingName=BuildingName;
     }
}

i just wanna know, was my method that i used above a correct method? maybe i have another problem i need to fix.

my criteria is , two objects are equal only if all of the attributes in one object are the same in another object.

Upvotes: 0

Views: 119

Answers (2)

mmelnik
mmelnik

Reputation: 741

The easiest way for you, as it was already mentioned - to implement ParkingList.equals() method. For example you can generate it by IDE.

Than your code:

temp.removeAll(ParkingsDB);

will work as you expected. This happens since list implementation basically depends on equals() method for checking elements.

You may also use streams:

ArrayList<ParkingList> temp = Parkings.stream()
    .filter(parking -> !ParkingsDB.contains(parking))
    .collect(Collectors.toList());

Upvotes: 0

Ousmane D.
Ousmane D.

Reputation: 56423

in order to utilise removeAll on a collection of custom types you'll need to provide an implementation of the equals method and if possible also the hashCode method as it is used by certain collections in the collection API.

another solution would be to utilise removeIf and specify the criteria which defines when two or more objects are equal.

e.g.

ArrayList<ParkingList> temp = new ArrayList(Parkings);
temp.removeIf(x -> ParkingsDB.stream()
                                 .anyMatch(e -> e.getName().equals(x.getName())));

in this case, the criteria is when any given object in temp has the same name as any given object in ParkingsDB then it shall be removed from the temp list.

now you'll simply need to decide whether to provide your own implementation of equals and hashCode or utilise the example above; in all cases, you'll need to provide a criteria which defines when two given objects are equal.


This is irrelevant to the problem at hand, but you don't seem to respect the Java naming conventions at all.

variables as well as methods (except constructors which is a special type of a method) should start with a lowercase letter and follow the camelCase naming convention i.e rather than Parkings it should be parkings, rather than Name it should be name etc.

Also, you seem to have freely exposed the state of ParkingList. you should enforce encapsulation here by making all the variables private and only provide getters and setters where necessary.

Upvotes: 1

Related Questions