Compareto () function usage in Java-comparable

Source: Internet
Author: User
Tags comparable
Comparetopublic int compareto (string anotherstring) compares two strings alphabetically. This comparison is based on the Unicode values of each character in the string. Compare the Character Sequence represented by this string object with the character sequence represented by the parameter string. If the string object is in alphabetical order before the parameter string, the comparison result is a negative integer. If the string object is placed after the parameter string in alphabetical order, the comparison result is a positive integer. If the two strings are equal, the result is 0. compareto returns 0 only when the equals (object) method returns true. This is the definition of dictionary sorting. If the two strings are different, either they have different characters at an index, the index is a valid index for both, or their length is different, or both of the above two conditions are met. If they have different characters at one or more index locations, assume K is the minimum value of such indexes; the string determined by the <operator has a smaller value on Position K, and its dictionary order is prior to other strings. In this case, compareto returns two different char values of the two strings at position K, that is, the value: This. charat (k)-anotherstring. charat (k)
If they do not have different index locations, the shorter strings are placed before the longer strings in the dictionary order. In this case, compareto returns the length difference between the two strings, that is, the value: This. Length ()-anotherstring. Length ()
Specified by: compareto parameter in the comparable <string> interface: anotherstring-string to be compared. Return Value: If the parameter string is equal to this string, 0 is returned. If the value of this string is less than the string parameter in alphabetical order, a value smaller than 0 is returned; if the value of this string is greater than the string parameter in alphabetical order, a value greater than 0 is returned.
Example:
public class ObjectCompareTo {
  private Integer min,max;
 public ObjectCompareTo(Integer min, Integer max) {
   this.min = min;
   this.max = max;
 }
  public boolean compareTo(Object num){
  Comparable compare=(Comparable)num;
  int minMatch=compare.compareTo(getMinComparable());
   int maxMatch=compare.compareTo(getMaxComparable());
   System.out.println(minMatch+"|"+maxMatch);
   return minMatch>=0&&maxMatch<=0;
 }
 public Comparable getMinComparable(){
  return this.min;
 }
 public Comparable getMaxComparable(){
  return this.max;
 }
 
 public static void main(String[] args) {
  Integer minValue=3,maxValue=25;
  ObjectCompareTo compareTo=new ObjectCompareTo(minValue,maxValue);
  System.out.println(compareTo.compareTo(24));
 }
}
Bytes

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.