Link Details

Link 1180683 thumbnail
User 1199191 avatar

By konstantina
via examples.javacodegeeks.com
Published: Jul 18 2014 / 04:09

In this example we will show how to use java.util.Comparator interface. java.util.Comparator is an interface which is used for sorting objects in Java. The compare(Object o1, Object o2) method of Comparator interface needs to be implemented for this purpose, which compares two objects and returns an integer, depending on the comparison: positive, if o1 is greater than o2; zero, if o1 equals to o2; negative, if o1 is less than o1. Another interface which is used for sorting objects is java.lang.Comparable. However, Comparable cannot sort objects on different attributes, while Comparator can. Also, the method compareTo(T o) of Comparable needs to be implemented in this case, which compares the current object (this object) with the specified object for order. In this example, we will pay attention to Comparator interface.
  • 13
  • 0
  • 1236
  • 3838

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.

Apache Hadoop
Written by: Piotr Krewski
Featured Refcardz: Top Refcardz:
  1. Play
  2. Akka
  3. Design Patterns
  4. OO JS
  5. Cont. Delivery
  1. Play
  2. Java Performance
  3. Akka
  4. REST
  5. Java