[Solved] How to keep only the object with the maximum value in the ArrayList?


This should be working.

List<Counter> toRemove = new ArrayList<Counter>();

Collections.sort(interval, (first, second) -> {

        int c = 0;

        if (first.compareWith(second)) {
            if (first.getCount() <= second.getCount()())
                toRemove.add(first);
            else if (first.getCount() >= second.getCount())
                toRemove.add(second);
        } else 
            c = first.getCount().compareTo(second.getCount());

        return c;
    }
);

interval.removeAll(toRemove);

This is the compareWith function in the Counter class.

public boolean compareWith(Counter second) {

    if (this.getStart().equals(second.getStart()) 
            && this.getEnd().equals(second.getEnd())
            && this.getStart().equals(second.getEnd())) {
        return true;
    }

    return false;
}

solved How to keep only the object with the maximum value in the ArrayList?