java - Explaination on CompareTo method for generics -


I have created a generic class with a type of variable, which implements java.lang. Because I'm having problems with implementing the () method because & lt ;, operators are not valid for normal type use. I came to it.

After the accepted answer I was able to apply the method to compare, but I do not understand how it works, please someone can explain .... the answer I was talking about ...

  Public square DoubleKey & lt; K extends widely & lt; K & gt ;, J extends & lt; J & gt; & Gt; Applies Comparable & lt; DoubleKey & lt; K, J & gt; & Gt; {Key of private 1; Private J key 2; Public Double Key (key 1, J key 2) {this.key1 = key1; This.key2 = key2; } Public Kashmir Festke () {this .key1; } Public J Late Secondhand () {return.key2; } Compare public intervals (Double key  

The answer code you mentioned is correct

I'll explain the code to you in a broad way. First of all, let's check the definition.

  Public Class Double and Lieutenant; Comparative in Kashmir and lieutenant; K & gt; Expands J, Comparative Lift; J & gt; & Gt; Applies Comparable & lt; DoubleKey & lt; K, J & gt; & Gt; It is true that the comparative itself is a normal interface and is being implemented by  comparative  by the  double class.   Pay attention to the way the type parameter K and J are decelled by double class and then the comparative interface is passed, it is ok to declare this way ! I hope this clears your first doubt! 

Next, your second doubt is Call again in comparison () method ! Although it seems apparently at first sight, but this is not at all. On careful decision you will find that it will not be recursive because the code will not be called due to the lack of object.

To make it more clear,

  compare public int (double key  that) {int cmp = this.getFirstKey () . Compare (that.getFirstKey ()); If (cmp == 0) cmp = this.getSecondKey () Compare (that.getSecondKey ()); Return cmp; }  

It's calling this (current calling object) first, then calling getFirstKey () and Then, compare () method of the standard comparative interface. Next, the parameter is defined as the compareto () method in the form of the external compareTo () method that.getgetSecondarykey () . It does not match the external () method as the external type () method is the argument of DoubleKey while the internal compareTo () method has the argument of type Therefore, Now, obviously, the standards are passed for external and inner compareto () the methods are different. To compare inner () comparisons in the comparative interface as standard compare () and for this reason the author has implemented a comparative interface!

For more clarity, I am simplifying the parameters for both methods: -

  External Comparison () (Double Key  K, J & gt; That) // parameter is of double type & lt; K, J & gt; Internal Inner Method  

First Comparison for i () (Key1) Hope this will answer all your doubts. Please comment if you could not understand! Also, try to mislead you at that time. Answer is correct and correct in my reply !!!


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -