You do not need to use method calls in the class, even if this field was private, which is not always known - private restricts access to the class, and not to the object.
Since your method returns nothing but returns an attribute, you can directly use the attribute:
@Override public int compare(ServerInfo o1, ServerInfo o2) { double datarate1=o1.serverDataRate; double datarate2=o2.serverDataRate; if (datarate1 > datarate2) return -1; else if ( datarate1 < datarate2) return +1; else return 0; }
But the JVM can optimize the function call, and in the range of 100 elements it is unlikely to be measurable.
Your method returns double - can you explain why?
With ints, you can simply do:
@Override public int compare (ServerInfo o1, ServerInfo o2) { return o2.serverDataRate - o1.serverDataRate; }
But consider the most extreme possible values ββfor int over- and underrun questions.
user unknown
source share