今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
Comparators
- [Instructor] If we would like to sort a collection using something besides its element's natural order, we can define our own comparator. So, let's imagine that we wanted to compare our rooms using their daily rate. To do this, we can build a comparator that overrides the natural order of the room. This comparison might be something that we use frequently, so I'm going to create the comparator as a static field inside of the room class. So I'll declare the field with a type of comparator, and then you'll recall that comparators are a generic type, so I'll specify room as its generic type. I'm going to name this the RATE_COMPARATOR, and then we can begin to create our comparator. To do that, I'm going to use the static methods found on the comparator that were added in Java 8. The first is the comparing method. This allows us to provide a field on the room that we would like to use for our comparison. So, in this case, I'm…