The Comparator interface is used to sort the elements of collections and arrays. It differs from the Comparable interface in that the Comparable interface sorts the elements only in the natural order. In contrast, the Comparator interface sorts the collections and arrays in a number of different ways.
The Comparator interface has a single compare() method, which returns an integer. It has the following signature:
- negative if Obj1 < Obj2
- positive if Obj1 > Obj2
- zero if Obj1 == Obj2
The Comparator interface can sort only the mutually comparable objects. It cannot be used to sort primitives. Sorting elements using the Comparator interface requires building a class separate from the class whose instances are to be sorted. This is in contrast to the Comparable interface, in which case the class to be sorted itself uses the Comparable interface.
- Get certified in first attempt download master-ciw-enterprise-developer - Master CIW Enterprise Developer simulation.
- Download free practice test for SUN SCBCD exam.
- Click here to download scdjws test study guide and practice question.
- Click here to download scjp test study guide and practice question.
- Click here to get free scjp-5-0 SCJP 5/6 exam practice questions.
- Download free practice test for SUN SCMAD exam.
- Download free practice test for scwcd SCWCD exam.
- Best exam simulation SUN CX310-055 download free trial.
- Get certified in first attempt download CX310-056 - SCJP 5.0 Java Upgrade simulation.
- Download practice question and study guide for CX310-065 for exam.
- Pass SCJP Upgrade in first attampt.
If you like this article, please leave a comment or subscribe this blog via RSS or via e-mail, Bookmark and share through your network. Click the AddThis button below. Thanks.
