Wednesday, 19 June 2019

How to Remove the Duplicates from ArrayList with Objects ?

List<Event> events = // fill with your events.
List<Event> noRepeat = new ArrayList<Event>();

for (Event event : events ) {    boolean isFound = false;
    // check if the event name exists in noRepeat
    for (Event e : noRepeat) {
        if (e.getName().equals(event.getName()) || (e.equals(event))) {
            isFound = true;        
            break;
        }
    }
    if (!isFound) noRepeat.add(event);
}


No comments:

Post a Comment