I am trying to make a very concise way (using streams, of course) to determine if two members of a collection (a list) are equal. But not equal in the sense of the method equals()
but using a different method for comparing.
Basically, I have a list of timeslots, and these define an order. You can say if a timeslot precedes, passes or is equal to another using the Timeslot#compareTo(Timeslot)
method. I need these timeslots to be in order, that is, every one of them precedes the following in the list, but I also need each one of them to be strictly greater than the next one. They cannot be equal (you cannot have timeslot1=10am
and then timeslot2=10am
).
I got the ordering covered, as easy as this:
Collections.sort(timeslots, Timeslot::compareTo);
However, I would like to be very concise when determining if every timeslot strictly precedes the following or not. Of course, I could do this the traditional way as follows:
for (int i = 0; i < timeslots.size() - 1; i++)
if (timeslots.get(i).compareTo(timeslots.get(i + 1)) == 0)
throw new IllegalArgumentException("Every timeslot must strictly precede the following");
Maybe I am asking too much of streams, and there is really no better and equally efficient way of achieving this, but I would be glad to hear out your ideas.